Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 1 | //===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass statically checks for common and easily-identified constructs |
| 11 | // which produce undefined or likely unintended behavior in LLVM IR. |
| 12 | // |
| 13 | // It is not a guarantee of correctness, in two ways. First, it isn't |
| 14 | // comprehensive. There are checks which could be done statically which are |
| 15 | // not yet implemented. Some of these are indicated by TODO comments, but |
| 16 | // those aren't comprehensive either. Second, many conditions cannot be |
| 17 | // checked statically. This pass does no dynamic instrumentation, so it |
| 18 | // can't check for all possible problems. |
Matt Arsenault | 38c18ef | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 19 | // |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 20 | // Another limitation is that it assumes all code will be executed. A store |
| 21 | // through a null pointer in a basic block which is never reached is harmless, |
Dan Gohman | d3b6e41 | 2010-07-06 15:21:57 +0000 | [diff] [blame] | 22 | // but this pass will warn about it anyway. This is the main reason why most |
| 23 | // of these checks live here instead of in the Verifier pass. |
Dan Gohman | 0883355 | 2010-04-22 01:30:05 +0000 | [diff] [blame] | 24 | // |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 25 | // Optimization passes may make conditions that this pass checks for more or |
| 26 | // less obvious. If an optimization pass appears to be introducing a warning, |
| 27 | // it may be that the optimization pass is merely exposing an existing |
| 28 | // condition in the code. |
Matt Arsenault | 38c18ef | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 29 | // |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 30 | // This code may be run before instcombine. In many cases, instcombine checks |
| 31 | // for the same kinds of things and turns instructions with undefined behavior |
| 32 | // into unreachable (or equivalent). Because of this, this pass makes some |
| 33 | // effort to look through bitcasts and so on. |
Matt Arsenault | 38c18ef | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 34 | // |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/Lint.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/APInt.h" |
| 39 | #include "llvm/ADT/ArrayRef.h" |
| 40 | #include "llvm/ADT/SmallPtrSet.h" |
| 41 | #include "llvm/ADT/Twine.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/AssumptionCache.h" |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/ConstantFolding.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/InstructionSimplify.h" |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/Loads.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/MemoryLocation.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | bda1349 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 51 | #include "llvm/IR/Argument.h" |
| 52 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 4bbfbdf | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 53 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 54 | #include "llvm/IR/Constant.h" |
| 55 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 56 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 57 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 56e1394 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 58 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 59 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 60 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 67f6bf7 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 61 | #include "llvm/IR/InstVisitor.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 62 | #include "llvm/IR/InstrTypes.h" |
| 63 | #include "llvm/IR/Instruction.h" |
| 64 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 65 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 66 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Module.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 68 | #include "llvm/IR/Type.h" |
| 69 | #include "llvm/IR/Value.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 70 | #include "llvm/Pass.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 71 | #include "llvm/Support/Casting.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 72 | #include "llvm/Support/Debug.h" |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 73 | #include "llvm/Support/KnownBits.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 74 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 75 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 76 | #include <cassert> |
| 77 | #include <cstdint> |
| 78 | #include <iterator> |
| 79 | #include <string> |
| 80 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 81 | using namespace llvm; |
| 82 | |
| 83 | namespace { |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 84 | namespace MemRef { |
Benjamin Kramer | c8a95a8 | 2015-03-08 16:07:39 +0000 | [diff] [blame] | 85 | static const unsigned Read = 1; |
| 86 | static const unsigned Write = 2; |
| 87 | static const unsigned Callee = 4; |
| 88 | static const unsigned Branchee = 8; |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 89 | } // end namespace MemRef |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 90 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 91 | class Lint : public FunctionPass, public InstVisitor<Lint> { |
| 92 | friend class InstVisitor<Lint>; |
| 93 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 94 | void visitFunction(Function &F); |
| 95 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 96 | void visitCallSite(CallSite CS); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 97 | void visitMemoryReference(Instruction &I, Value *Ptr, |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 98 | uint64_t Size, unsigned Align, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 99 | Type *Ty, unsigned Flags); |
Andrew Kaylor | 7741851 | 2015-02-10 19:52:43 +0000 | [diff] [blame] | 100 | void visitEHBeginCatch(IntrinsicInst *II); |
| 101 | void visitEHEndCatch(IntrinsicInst *II); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 102 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 103 | void visitCallInst(CallInst &I); |
| 104 | void visitInvokeInst(InvokeInst &I); |
| 105 | void visitReturnInst(ReturnInst &I); |
| 106 | void visitLoadInst(LoadInst &I); |
| 107 | void visitStoreInst(StoreInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 108 | void visitXor(BinaryOperator &I); |
| 109 | void visitSub(BinaryOperator &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 110 | void visitLShr(BinaryOperator &I); |
| 111 | void visitAShr(BinaryOperator &I); |
| 112 | void visitShl(BinaryOperator &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 113 | void visitSDiv(BinaryOperator &I); |
| 114 | void visitUDiv(BinaryOperator &I); |
| 115 | void visitSRem(BinaryOperator &I); |
| 116 | void visitURem(BinaryOperator &I); |
| 117 | void visitAllocaInst(AllocaInst &I); |
| 118 | void visitVAArgInst(VAArgInst &I); |
| 119 | void visitIndirectBrInst(IndirectBrInst &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 120 | void visitExtractElementInst(ExtractElementInst &I); |
| 121 | void visitInsertElementInst(InsertElementInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 122 | void visitUnreachableInst(UnreachableInst &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 123 | |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 124 | Value *findValue(Value *V, bool OffsetOk) const; |
| 125 | Value *findValueImpl(Value *V, bool OffsetOk, |
Craig Topper | 431bdfc | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 126 | SmallPtrSetImpl<Value *> &Visited) const; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 127 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 128 | public: |
| 129 | Module *Mod; |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 130 | const DataLayout *DL; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 131 | AliasAnalysis *AA; |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 132 | AssumptionCache *AC; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 133 | DominatorTree *DT; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 134 | TargetLibraryInfo *TLI; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 135 | |
Alp Toker | 8dd8d5c | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 136 | std::string Messages; |
| 137 | raw_string_ostream MessagesStr; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 138 | |
| 139 | static char ID; // Pass identification, replacement for typeid |
Alp Toker | 8dd8d5c | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 140 | Lint() : FunctionPass(ID), MessagesStr(Messages) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 141 | initializeLintPass(*PassRegistry::getPassRegistry()); |
| 142 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 143 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 144 | bool runOnFunction(Function &F) override; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 145 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 146 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 147 | AU.setPreservesAll(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 148 | AU.addRequired<AAResultsWrapperPass>(); |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 149 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | eeeec3c | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 150 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 7f2eff7 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 151 | AU.addRequired<DominatorTreeWrapperPass>(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 152 | } |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 153 | void print(raw_ostream &O, const Module *M) const override {} |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 154 | |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 155 | void WriteValues(ArrayRef<const Value *> Vs) { |
| 156 | for (const Value *V : Vs) { |
| 157 | if (!V) |
| 158 | continue; |
| 159 | if (isa<Instruction>(V)) { |
| 160 | MessagesStr << *V << '\n'; |
| 161 | } else { |
| 162 | V->printAsOperand(MessagesStr, true, Mod); |
| 163 | MessagesStr << '\n'; |
| 164 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 168 | /// A check failed, so printout out the condition and the message. |
Duncan P. N. Exon Smith | a718967 | 2015-03-16 17:49:03 +0000 | [diff] [blame] | 169 | /// |
| 170 | /// This provides a nice place to put a breakpoint if you want to see why |
| 171 | /// something is not correct. |
Duncan P. N. Exon Smith | f141a55 | 2015-03-14 16:47:37 +0000 | [diff] [blame] | 172 | void CheckFailed(const Twine &Message) { MessagesStr << Message << '\n'; } |
| 173 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 174 | /// A check failed (with values to print). |
Duncan P. N. Exon Smith | a718967 | 2015-03-16 17:49:03 +0000 | [diff] [blame] | 175 | /// |
| 176 | /// This calls the Message-only version so that the above is easier to set |
| 177 | /// a breakpoint on. |
Duncan P. N. Exon Smith | f141a55 | 2015-03-14 16:47:37 +0000 | [diff] [blame] | 178 | template <typename T1, typename... Ts> |
| 179 | void CheckFailed(const Twine &Message, const T1 &V1, const Ts &...Vs) { |
| 180 | CheckFailed(Message); |
| 181 | WriteValues({V1, Vs...}); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 182 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 183 | }; |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 184 | } // end anonymous namespace |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 185 | |
| 186 | char Lint::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 187 | INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR", |
| 188 | false, true) |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 189 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | eeeec3c | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 190 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chandler Carruth | 7f2eff7 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 191 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 192 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 193 | INITIALIZE_PASS_END(Lint, "lint", "Statically lint-checks LLVM IR", |
| 194 | false, true) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 195 | |
| 196 | // Assert - We know that cond should be true, if not print an error message. |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 197 | #define Assert(C, ...) \ |
Eugene Zelenko | 82d16ec | 2016-08-13 00:50:41 +0000 | [diff] [blame] | 198 | do { if (!(C)) { CheckFailed(__VA_ARGS__); return; } } while (false) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 199 | |
| 200 | // Lint::run - This is the main Analysis entry point for a |
| 201 | // function. |
| 202 | // |
| 203 | bool Lint::runOnFunction(Function &F) { |
| 204 | Mod = F.getParent(); |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 205 | DL = &F.getParent()->getDataLayout(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 206 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 207 | AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Chandler Carruth | 7f2eff7 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 208 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chandler Carruth | eeeec3c | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 209 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 210 | visit(F); |
| 211 | dbgs() << MessagesStr.str(); |
Alp Toker | 8dd8d5c | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 212 | Messages.clear(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 216 | void Lint::visitFunction(Function &F) { |
| 217 | // This isn't undefined behavior, it's just a little unusual, and it's a |
| 218 | // fairly common mistake to neglect to name a function. |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 219 | Assert(F.hasName() || F.hasLocalLinkage(), |
| 220 | "Unusual: Unnamed function with non-local linkage", &F); |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 221 | |
| 222 | // TODO: Check for irreducible control flow. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void Lint::visitCallSite(CallSite CS) { |
| 226 | Instruction &I = *CS.getInstruction(); |
| 227 | Value *Callee = CS.getCalledValue(); |
| 228 | |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 229 | visitMemoryReference(I, Callee, MemoryLocation::UnknownSize, 0, nullptr, |
| 230 | MemRef::Callee); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 231 | |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 232 | if (Function *F = dyn_cast<Function>(findValue(Callee, |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 233 | /*OffsetOk=*/false))) { |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 234 | Assert(CS.getCallingConv() == F->getCallingConv(), |
| 235 | "Undefined behavior: Caller and callee calling convention differ", |
| 236 | &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 237 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 238 | FunctionType *FT = F->getFunctionType(); |
Matt Arsenault | 20f1fe5 | 2013-11-10 03:18:50 +0000 | [diff] [blame] | 239 | unsigned NumActualArgs = CS.arg_size(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 240 | |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 241 | Assert(FT->isVarArg() ? FT->getNumParams() <= NumActualArgs |
| 242 | : FT->getNumParams() == NumActualArgs, |
| 243 | "Undefined behavior: Call argument count mismatches callee " |
| 244 | "argument count", |
| 245 | &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 246 | |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 247 | Assert(FT->getReturnType() == I.getType(), |
| 248 | "Undefined behavior: Call return type mismatches " |
| 249 | "callee return type", |
| 250 | &I); |
Dan Gohman | 545d006 | 2010-07-12 18:02:04 +0000 | [diff] [blame] | 251 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 252 | // Check argument types (in case the callee was casted) and attributes. |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 253 | // TODO: Verify that caller and callee attributes are compatible. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 254 | Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end(); |
| 255 | CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 256 | for (; AI != AE; ++AI) { |
| 257 | Value *Actual = *AI; |
| 258 | if (PI != PE) { |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 259 | Argument *Formal = &*PI++; |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 260 | Assert(Formal->getType() == Actual->getType(), |
| 261 | "Undefined behavior: Call argument type mismatches " |
| 262 | "callee parameter type", |
| 263 | &I); |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 264 | |
Dan Gohman | c1f1efd | 2010-12-13 22:53:18 +0000 | [diff] [blame] | 265 | // Check that noalias arguments don't alias other arguments. This is |
| 266 | // not fully precise because we don't know the sizes of the dereferenced |
| 267 | // memory regions. |
Mikael Holmen | e7f98a9 | 2017-12-27 08:48:33 +0000 | [diff] [blame] | 268 | if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) { |
| 269 | AttributeList PAL = CS.getAttributes(); |
| 270 | unsigned ArgNo = 0; |
| 271 | for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI) { |
| 272 | // Skip ByVal arguments since they will be memcpy'd to the callee's |
| 273 | // stack so we're not really passing the pointer anyway. |
| 274 | if (PAL.hasParamAttribute(ArgNo++, Attribute::ByVal)) |
| 275 | continue; |
Dan Gohman | 4a2a3ea | 2010-12-10 20:04:06 +0000 | [diff] [blame] | 276 | if (AI != BI && (*BI)->getType()->isPointerTy()) { |
Chandler Carruth | 1e3557d | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 277 | AliasResult Result = AA->alias(*AI, *BI); |
| 278 | Assert(Result != MustAlias && Result != PartialAlias, |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 279 | "Unusual: noalias argument aliases another argument", &I); |
Dan Gohman | 4a2a3ea | 2010-12-10 20:04:06 +0000 | [diff] [blame] | 280 | } |
Mikael Holmen | e7f98a9 | 2017-12-27 08:48:33 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 283 | |
| 284 | // Check that an sret argument points to valid memory. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 285 | if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 286 | Type *Ty = |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 287 | cast<PointerType>(Formal->getType())->getElementType(); |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 288 | visitMemoryReference(I, Actual, DL->getTypeStoreSize(Ty), |
| 289 | DL->getABITypeAlignment(Ty), Ty, |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 290 | MemRef::Read | MemRef::Write); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Mikael Holmen | c14b5c3 | 2017-11-15 07:46:48 +0000 | [diff] [blame] | 296 | if (CS.isCall()) { |
| 297 | const CallInst *CI = cast<CallInst>(CS.getInstruction()); |
| 298 | if (CI->isTailCall()) { |
| 299 | const AttributeList &PAL = CI->getAttributes(); |
| 300 | unsigned ArgNo = 0; |
| 301 | for (Value *Arg : CS.args()) { |
| 302 | // Skip ByVal arguments since they will be memcpy'd to the callee's |
| 303 | // stack anyway. |
| 304 | if (PAL.hasParamAttribute(ArgNo++, Attribute::ByVal)) |
| 305 | continue; |
| 306 | Value *Obj = findValue(Arg, /*OffsetOk=*/true); |
| 307 | Assert(!isa<AllocaInst>(Obj), |
| 308 | "Undefined behavior: Call with \"tail\" keyword references " |
| 309 | "alloca", |
| 310 | &I); |
| 311 | } |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 312 | } |
Mikael Holmen | c14b5c3 | 2017-11-15 07:46:48 +0000 | [diff] [blame] | 313 | } |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 314 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 315 | |
| 316 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) |
| 317 | switch (II->getIntrinsicID()) { |
| 318 | default: break; |
| 319 | |
| 320 | // TODO: Check more intrinsics |
| 321 | |
| 322 | case Intrinsic::memcpy: { |
| 323 | MemCpyInst *MCI = cast<MemCpyInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 324 | // TODO: If the size is known, use it. |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 325 | visitMemoryReference(I, MCI->getDest(), MemoryLocation::UnknownSize, |
Daniel Neilson | 8ff1aed | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 326 | MCI->getDestAlignment(), nullptr, MemRef::Write); |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 327 | visitMemoryReference(I, MCI->getSource(), MemoryLocation::UnknownSize, |
Daniel Neilson | 8ff1aed | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 328 | MCI->getSourceAlignment(), nullptr, MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 329 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 330 | // Check that the memcpy arguments don't overlap. The AliasAnalysis API |
| 331 | // isn't expressive enough for what we really want to do. Known partial |
| 332 | // overlap is not distinguished from the case where nothing is known. |
George Burgess IV | 1a44b0b | 2018-12-23 02:50:08 +0000 | [diff] [blame] | 333 | auto Size = LocationSize::unknown(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 334 | if (const ConstantInt *Len = |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 335 | dyn_cast<ConstantInt>(findValue(MCI->getLength(), |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 336 | /*OffsetOk=*/false))) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 337 | if (Len->getValue().isIntN(32)) |
George Burgess IV | 1a44b0b | 2018-12-23 02:50:08 +0000 | [diff] [blame] | 338 | Size = LocationSize::precise(Len->getValue().getZExtValue()); |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 339 | Assert(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) != |
Chandler Carruth | 1e3557d | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 340 | MustAlias, |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 341 | "Undefined behavior: memcpy source and destination overlap", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 342 | break; |
| 343 | } |
| 344 | case Intrinsic::memmove: { |
| 345 | MemMoveInst *MMI = cast<MemMoveInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 346 | // TODO: If the size is known, use it. |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 347 | visitMemoryReference(I, MMI->getDest(), MemoryLocation::UnknownSize, |
Daniel Neilson | 8ff1aed | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 348 | MMI->getDestAlignment(), nullptr, MemRef::Write); |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 349 | visitMemoryReference(I, MMI->getSource(), MemoryLocation::UnknownSize, |
Daniel Neilson | 8ff1aed | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 350 | MMI->getSourceAlignment(), nullptr, MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | case Intrinsic::memset: { |
| 354 | MemSetInst *MSI = cast<MemSetInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 355 | // TODO: If the size is known, use it. |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 356 | visitMemoryReference(I, MSI->getDest(), MemoryLocation::UnknownSize, |
Daniel Neilson | 8ff1aed | 2018-01-31 16:42:15 +0000 | [diff] [blame] | 357 | MSI->getDestAlignment(), nullptr, MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 358 | break; |
| 359 | } |
| 360 | |
| 361 | case Intrinsic::vastart: |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 362 | Assert(I.getParent()->getParent()->isVarArg(), |
| 363 | "Undefined behavior: va_start called in a non-varargs function", |
| 364 | &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 365 | |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 366 | visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0, |
| 367 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 368 | break; |
| 369 | case Intrinsic::vacopy: |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 370 | visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0, |
| 371 | nullptr, MemRef::Write); |
| 372 | visitMemoryReference(I, CS.getArgument(1), MemoryLocation::UnknownSize, 0, |
| 373 | nullptr, MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 374 | break; |
| 375 | case Intrinsic::vaend: |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 376 | visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0, |
| 377 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 378 | break; |
Dan Gohman | 882ddb4 | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 379 | |
| 380 | case Intrinsic::stackrestore: |
| 381 | // Stackrestore doesn't read or write memory, but it sets the |
| 382 | // stack pointer, which the compiler may read from or write to |
| 383 | // at any time, so check it for both readability and writeability. |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 384 | visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0, |
| 385 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 882ddb4 | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 386 | break; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | void Lint::visitCallInst(CallInst &I) { |
| 391 | return visitCallSite(&I); |
| 392 | } |
| 393 | |
| 394 | void Lint::visitInvokeInst(InvokeInst &I) { |
| 395 | return visitCallSite(&I); |
| 396 | } |
| 397 | |
| 398 | void Lint::visitReturnInst(ReturnInst &I) { |
| 399 | Function *F = I.getParent()->getParent(); |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 400 | Assert(!F->doesNotReturn(), |
| 401 | "Unusual: Return statement in function with noreturn attribute", &I); |
Dan Gohman | 292fc87 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 402 | |
| 403 | if (Value *V = I.getReturnValue()) { |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 404 | Value *Obj = findValue(V, /*OffsetOk=*/true); |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 405 | Assert(!isa<AllocaInst>(Obj), "Unusual: Returning alloca value", &I); |
Dan Gohman | 292fc87 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 406 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 409 | // TODO: Check that the reference is in bounds. |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 410 | // TODO: Check readnone/readonly function attributes. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 411 | void Lint::visitMemoryReference(Instruction &I, |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 412 | Value *Ptr, uint64_t Size, unsigned Align, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 413 | Type *Ty, unsigned Flags) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 414 | // If no memory is being referenced, it doesn't matter if the pointer |
| 415 | // is valid. |
| 416 | if (Size == 0) |
| 417 | return; |
| 418 | |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 419 | Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true); |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 420 | Assert(!isa<ConstantPointerNull>(UnderlyingObject), |
| 421 | "Undefined behavior: Null pointer dereference", &I); |
| 422 | Assert(!isa<UndefValue>(UnderlyingObject), |
| 423 | "Undefined behavior: Undef pointer dereference", &I); |
| 424 | Assert(!isa<ConstantInt>(UnderlyingObject) || |
Craig Topper | 6dbd34d | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 425 | !cast<ConstantInt>(UnderlyingObject)->isMinusOne(), |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 426 | "Unusual: All-ones pointer dereference", &I); |
| 427 | Assert(!isa<ConstantInt>(UnderlyingObject) || |
| 428 | !cast<ConstantInt>(UnderlyingObject)->isOne(), |
| 429 | "Unusual: Address one pointer dereference", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 430 | |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 431 | if (Flags & MemRef::Write) { |
| 432 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject)) |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 433 | Assert(!GV->isConstant(), "Undefined behavior: Write to read-only memory", |
| 434 | &I); |
| 435 | Assert(!isa<Function>(UnderlyingObject) && |
| 436 | !isa<BlockAddress>(UnderlyingObject), |
| 437 | "Undefined behavior: Write to text section", &I); |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 438 | } |
| 439 | if (Flags & MemRef::Read) { |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 440 | Assert(!isa<Function>(UnderlyingObject), "Unusual: Load from function body", |
| 441 | &I); |
| 442 | Assert(!isa<BlockAddress>(UnderlyingObject), |
| 443 | "Undefined behavior: Load from block address", &I); |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 444 | } |
| 445 | if (Flags & MemRef::Callee) { |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 446 | Assert(!isa<BlockAddress>(UnderlyingObject), |
| 447 | "Undefined behavior: Call to block address", &I); |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 448 | } |
| 449 | if (Flags & MemRef::Branchee) { |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 450 | Assert(!isa<Constant>(UnderlyingObject) || |
| 451 | isa<BlockAddress>(UnderlyingObject), |
| 452 | "Undefined behavior: Branch to non-blockaddress", &I); |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Duncan Sands | c7c42f7 | 2012-09-26 07:45:36 +0000 | [diff] [blame] | 455 | // Check for buffer overflows and misalignment. |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 456 | // Only handles memory references that read/write something simple like an |
| 457 | // alloca instruction or a global variable. |
| 458 | int64_t Offset = 0; |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 459 | if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, *DL)) { |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 460 | // OK, so the access is to a constant offset from Ptr. Check that Ptr is |
| 461 | // something we can handle and if so extract the size of this base object |
| 462 | // along with its alignment. |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 463 | uint64_t BaseSize = MemoryLocation::UnknownSize; |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 464 | unsigned BaseAlign = 0; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 465 | |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 466 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { |
| 467 | Type *ATy = AI->getAllocatedType(); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 468 | if (!AI->isArrayAllocation() && ATy->isSized()) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 469 | BaseSize = DL->getTypeAllocSize(ATy); |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 470 | BaseAlign = AI->getAlignment(); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 471 | if (BaseAlign == 0 && ATy->isSized()) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 472 | BaseAlign = DL->getABITypeAlignment(ATy); |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 473 | } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
| 474 | // If the global may be defined differently in another compilation unit |
| 475 | // then don't warn about funky memory accesses. |
| 476 | if (GV->hasDefinitiveInitializer()) { |
Manuel Jacob | 75e1cfb | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 477 | Type *GTy = GV->getValueType(); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 478 | if (GTy->isSized()) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 479 | BaseSize = DL->getTypeAllocSize(GTy); |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 480 | BaseAlign = GV->getAlignment(); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 481 | if (BaseAlign == 0 && GTy->isSized()) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 482 | BaseAlign = DL->getABITypeAlignment(GTy); |
Duncan Sands | 00edf4c | 2012-09-25 10:00:49 +0000 | [diff] [blame] | 483 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 484 | } |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 485 | |
| 486 | // Accesses from before the start or after the end of the object are not |
| 487 | // defined. |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 488 | Assert(Size == MemoryLocation::UnknownSize || |
| 489 | BaseSize == MemoryLocation::UnknownSize || |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 490 | (Offset >= 0 && Offset + Size <= BaseSize), |
| 491 | "Undefined behavior: Buffer overflow", &I); |
Dan Gohman | a070d2a | 2013-01-31 02:00:45 +0000 | [diff] [blame] | 492 | |
| 493 | // Accesses that say that the memory is more aligned than it is are not |
| 494 | // defined. |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 495 | if (Align == 0 && Ty && Ty->isSized()) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 496 | Align = DL->getABITypeAlignment(Ty); |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 497 | Assert(!BaseAlign || Align <= MinAlign(BaseAlign, Offset), |
| 498 | "Undefined behavior: Memory reference address is misaligned", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | void Lint::visitLoadInst(LoadInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 503 | visitMemoryReference(I, I.getPointerOperand(), |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 504 | DL->getTypeStoreSize(I.getType()), I.getAlignment(), |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 505 | I.getType(), MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void Lint::visitStoreInst(StoreInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 509 | visitMemoryReference(I, I.getPointerOperand(), |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 510 | DL->getTypeStoreSize(I.getOperand(0)->getType()), |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 511 | I.getAlignment(), |
| 512 | I.getOperand(0)->getType(), MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 515 | void Lint::visitXor(BinaryOperator &I) { |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 516 | Assert(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)), |
| 517 | "Undefined result: xor(undef, undef)", &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | void Lint::visitSub(BinaryOperator &I) { |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 521 | Assert(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)), |
| 522 | "Undefined result: sub(undef, undef)", &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 525 | void Lint::visitLShr(BinaryOperator &I) { |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 526 | if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(1), |
| 527 | /*OffsetOk=*/false))) |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 528 | Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
| 529 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void Lint::visitAShr(BinaryOperator &I) { |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 533 | if (ConstantInt *CI = |
| 534 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 535 | Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
| 536 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | void Lint::visitShl(BinaryOperator &I) { |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 540 | if (ConstantInt *CI = |
| 541 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 542 | Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
| 543 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 546 | static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, |
| 547 | AssumptionCache *AC) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 548 | // Assume undef could be zero. |
Matt Arsenault | e3e5f77 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 549 | if (isa<UndefValue>(V)) |
| 550 | return true; |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 551 | |
Matt Arsenault | e3e5f77 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 552 | VectorType *VecTy = dyn_cast<VectorType>(V->getType()); |
| 553 | if (!VecTy) { |
Craig Topper | e3a1116 | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 554 | KnownBits Known = computeKnownBits(V, DL, 0, AC, dyn_cast<Instruction>(V), DT); |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 555 | return Known.isZero(); |
Matt Arsenault | e3e5f77 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // Per-component check doesn't work with zeroinitializer |
| 559 | Constant *C = dyn_cast<Constant>(V); |
| 560 | if (!C) |
| 561 | return false; |
| 562 | |
| 563 | if (C->isZeroValue()) |
| 564 | return true; |
| 565 | |
| 566 | // For a vector, KnownZero will only be true if all values are zero, so check |
| 567 | // this per component |
Matt Arsenault | e3e5f77 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 568 | for (unsigned I = 0, N = VecTy->getNumElements(); I != N; ++I) { |
| 569 | Constant *Elem = C->getAggregateElement(I); |
| 570 | if (isa<UndefValue>(Elem)) |
| 571 | return true; |
| 572 | |
Craig Topper | e3a1116 | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 573 | KnownBits Known = computeKnownBits(Elem, DL); |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 574 | if (Known.isZero()) |
Matt Arsenault | e3e5f77 | 2013-08-26 23:29:33 +0000 | [diff] [blame] | 575 | return true; |
| 576 | } |
| 577 | |
| 578 | return false; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | void Lint::visitSDiv(BinaryOperator &I) { |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 582 | Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC), |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 583 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void Lint::visitUDiv(BinaryOperator &I) { |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 587 | Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC), |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 588 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void Lint::visitSRem(BinaryOperator &I) { |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 592 | Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC), |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 593 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | void Lint::visitURem(BinaryOperator &I) { |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 597 | Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC), |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 598 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void Lint::visitAllocaInst(AllocaInst &I) { |
| 602 | if (isa<ConstantInt>(I.getArraySize())) |
| 603 | // This isn't undefined behavior, it's just an obvious pessimization. |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 604 | Assert(&I.getParent()->getParent()->getEntryBlock() == I.getParent(), |
| 605 | "Pessimization: Static alloca outside of entry block", &I); |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 606 | |
| 607 | // TODO: Check for an unusual size (MSB set?) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void Lint::visitVAArgInst(VAArgInst &I) { |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 611 | visitMemoryReference(I, I.getOperand(0), MemoryLocation::UnknownSize, 0, |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 612 | nullptr, MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | void Lint::visitIndirectBrInst(IndirectBrInst &I) { |
Chandler Carruth | 2cdca0c4 | 2015-06-17 07:21:38 +0000 | [diff] [blame] | 616 | visitMemoryReference(I, I.getAddress(), MemoryLocation::UnknownSize, 0, |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 617 | nullptr, MemRef::Branchee); |
Dan Gohman | a8afb2a | 2010-08-02 23:06:43 +0000 | [diff] [blame] | 618 | |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 619 | Assert(I.getNumDestinations() != 0, |
| 620 | "Undefined behavior: indirectbr with no destinations", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 623 | void Lint::visitExtractElementInst(ExtractElementInst &I) { |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 624 | if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getIndexOperand(), |
| 625 | /*OffsetOk=*/false))) |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 626 | Assert(CI->getValue().ult(I.getVectorOperandType()->getNumElements()), |
| 627 | "Undefined result: extractelement index out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void Lint::visitInsertElementInst(InsertElementInst &I) { |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 631 | if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(2), |
| 632 | /*OffsetOk=*/false))) |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 633 | Assert(CI->getValue().ult(I.getType()->getNumElements()), |
| 634 | "Undefined result: insertelement index out of range", &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | void Lint::visitUnreachableInst(UnreachableInst &I) { |
| 638 | // This isn't undefined behavior, it's merely suspicious. |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 639 | Assert(&I == &I.getParent()->front() || |
| 640 | std::prev(I.getIterator())->mayHaveSideEffects(), |
Benjamin Kramer | fab98a4 | 2015-03-07 21:15:40 +0000 | [diff] [blame] | 641 | "Unusual: unreachable immediately preceded by instruction without " |
| 642 | "side effects", |
| 643 | &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 646 | /// findValue - Look through bitcasts and simple memory reference patterns |
| 647 | /// to identify an equivalent, but more informative, value. If OffsetOk |
| 648 | /// is true, look through getelementptrs with non-zero offsets too. |
| 649 | /// |
| 650 | /// Most analysis passes don't require this logic, because instcombine |
| 651 | /// will simplify most of these kinds of things away. But it's a goal of |
| 652 | /// this Lint pass to be useful even on non-optimized IR. |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 653 | Value *Lint::findValue(Value *V, bool OffsetOk) const { |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 654 | SmallPtrSet<Value *, 4> Visited; |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 655 | return findValueImpl(V, OffsetOk, Visited); |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /// findValueImpl - Implementation helper for findValue. |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 659 | Value *Lint::findValueImpl(Value *V, bool OffsetOk, |
Craig Topper | 431bdfc | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 660 | SmallPtrSetImpl<Value *> &Visited) const { |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 661 | // Detect self-referential values. |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 662 | if (!Visited.insert(V).second) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 663 | return UndefValue::get(V->getType()); |
| 664 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 665 | // TODO: Look through sext or zext cast, when the result is known to |
| 666 | // be interpreted as signed or unsigned, respectively. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 667 | // TODO: Look through eliminable cast pairs. |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 668 | // TODO: Look through calls with unique return values. |
| 669 | // TODO: Look through vector insert/extract/shuffle. |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 670 | V = OffsetOk ? GetUnderlyingObject(V, *DL) : V->stripPointerCasts(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 671 | if (LoadInst *L = dyn_cast<LoadInst>(V)) { |
Duncan P. N. Exon Smith | d3a5adc | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 672 | BasicBlock::iterator BBI = L->getIterator(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 673 | BasicBlock *BB = L->getParent(); |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 674 | SmallPtrSet<BasicBlock *, 4> VisitedBlocks; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 675 | for (;;) { |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 676 | if (!VisitedBlocks.insert(BB).second) |
| 677 | break; |
Larisse Voufo | 6932880 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 678 | if (Value *U = |
Eduard Burtescu | f79a031 | 2016-01-22 01:51:51 +0000 | [diff] [blame] | 679 | FindAvailableLoadedValue(L, BB, BBI, DefMaxInstsToScan, AA)) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 680 | return findValueImpl(U, OffsetOk, Visited); |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 681 | if (BBI != BB->begin()) break; |
| 682 | BB = BB->getUniquePredecessor(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 683 | if (!BB) break; |
| 684 | BBI = BB->end(); |
| 685 | } |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 686 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 687 | if (Value *W = PN->hasConstantValue()) |
Duncan Sands | 23a1957 | 2010-11-17 10:23:23 +0000 | [diff] [blame] | 688 | if (W != V) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 689 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 690 | } else if (CastInst *CI = dyn_cast<CastInst>(V)) { |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 691 | if (CI->isNoopCast(*DL)) |
| 692 | return findValueImpl(CI->getOperand(0), OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 693 | } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) { |
| 694 | if (Value *W = FindInsertedValue(Ex->getAggregateOperand(), |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 695 | Ex->getIndices())) |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 696 | if (W != V) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 697 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 698 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 699 | // Same as above, but for ConstantExpr instead of Instruction. |
| 700 | if (Instruction::isCast(CE->getOpcode())) { |
| 701 | if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()), |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 702 | CE->getOperand(0)->getType(), CE->getType(), |
Mikael Holmen | 42c9fd0 | 2017-10-03 06:03:49 +0000 | [diff] [blame] | 703 | *DL)) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 704 | return findValueImpl(CE->getOperand(0), OffsetOk, Visited); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 705 | } else if (CE->getOpcode() == Instruction::ExtractValue) { |
Jay Foad | d30aa5a | 2011-04-13 15:22:40 +0000 | [diff] [blame] | 706 | ArrayRef<unsigned> Indices = CE->getIndices(); |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 707 | if (Value *W = FindInsertedValue(CE->getOperand(0), Indices)) |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 708 | if (W != V) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 709 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 710 | } |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // As a last resort, try SimplifyInstruction or constant folding. |
| 714 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
Daniel Berlin | e2c5126 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 715 | if (Value *W = SimplifyInstruction(Inst, {*DL, TLI, DT, AC})) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 716 | return findValueImpl(W, OffsetOk, Visited); |
David Majnemer | e43ff14 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 717 | } else if (auto *C = dyn_cast<Constant>(V)) { |
| 718 | if (Value *W = ConstantFoldConstant(C, *DL, TLI)) |
| 719 | if (W && W != V) |
Chandler Carruth | da53004 | 2015-08-06 02:05:46 +0000 | [diff] [blame] | 720 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | return V; |
| 724 | } |
| 725 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 726 | //===----------------------------------------------------------------------===// |
| 727 | // Implement the public interfaces to this file... |
| 728 | //===----------------------------------------------------------------------===// |
| 729 | |
| 730 | FunctionPass *llvm::createLintPass() { |
| 731 | return new Lint(); |
| 732 | } |
| 733 | |
| 734 | /// lintFunction - Check a function for errors, printing messages on stderr. |
| 735 | /// |
| 736 | void llvm::lintFunction(const Function &f) { |
| 737 | Function &F = const_cast<Function&>(f); |
| 738 | assert(!F.isDeclaration() && "Cannot lint external functions"); |
| 739 | |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 740 | legacy::FunctionPassManager FPM(F.getParent()); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 741 | Lint *V = new Lint(); |
| 742 | FPM.add(V); |
| 743 | FPM.run(F); |
| 744 | } |
| 745 | |
| 746 | /// lintModule - Check a module for errors, printing messages on stderr. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 747 | /// |
Dan Gohman | a0f7ff3 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 748 | void llvm::lintModule(const Module &M) { |
Chandler Carruth | 417c5c1 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 749 | legacy::PassManager PM; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 750 | Lint *V = new Lint(); |
| 751 | PM.add(V); |
| 752 | PM.run(const_cast<Module&>(M)); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 753 | } |