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. |
| 19 | // |
| 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. |
| 29 | // |
| 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. |
| 34 | // |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | #include "llvm/Analysis/Passes.h" |
| 38 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/InstructionSimplify.h" |
| 40 | #include "llvm/Analysis/ConstantFolding.h" |
| 41 | #include "llvm/Analysis/Dominators.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/Lint.h" |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/Loads.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/ValueTracking.h" |
| 45 | #include "llvm/Assembly/Writer.h" |
| 46 | #include "llvm/Target/TargetData.h" |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetLibraryInfo.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 48 | #include "llvm/Pass.h" |
| 49 | #include "llvm/PassManager.h" |
| 50 | #include "llvm/IntrinsicInst.h" |
| 51 | #include "llvm/Function.h" |
| 52 | #include "llvm/Support/CallSite.h" |
| 53 | #include "llvm/Support/Debug.h" |
| 54 | #include "llvm/Support/InstVisitor.h" |
| 55 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 57 | using namespace llvm; |
| 58 | |
| 59 | namespace { |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 60 | namespace MemRef { |
| 61 | static unsigned Read = 1; |
| 62 | static unsigned Write = 2; |
| 63 | static unsigned Callee = 4; |
| 64 | static unsigned Branchee = 8; |
| 65 | } |
| 66 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 67 | class Lint : public FunctionPass, public InstVisitor<Lint> { |
| 68 | friend class InstVisitor<Lint>; |
| 69 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 70 | void visitFunction(Function &F); |
| 71 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 72 | void visitCallSite(CallSite CS); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 73 | void visitMemoryReference(Instruction &I, Value *Ptr, |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 74 | uint64_t Size, unsigned Align, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 75 | Type *Ty, unsigned Flags); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 76 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 77 | void visitCallInst(CallInst &I); |
| 78 | void visitInvokeInst(InvokeInst &I); |
| 79 | void visitReturnInst(ReturnInst &I); |
| 80 | void visitLoadInst(LoadInst &I); |
| 81 | void visitStoreInst(StoreInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 82 | void visitXor(BinaryOperator &I); |
| 83 | void visitSub(BinaryOperator &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 84 | void visitLShr(BinaryOperator &I); |
| 85 | void visitAShr(BinaryOperator &I); |
| 86 | void visitShl(BinaryOperator &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 87 | void visitSDiv(BinaryOperator &I); |
| 88 | void visitUDiv(BinaryOperator &I); |
| 89 | void visitSRem(BinaryOperator &I); |
| 90 | void visitURem(BinaryOperator &I); |
| 91 | void visitAllocaInst(AllocaInst &I); |
| 92 | void visitVAArgInst(VAArgInst &I); |
| 93 | void visitIndirectBrInst(IndirectBrInst &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 94 | void visitExtractElementInst(ExtractElementInst &I); |
| 95 | void visitInsertElementInst(InsertElementInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 96 | void visitUnreachableInst(UnreachableInst &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 97 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 98 | Value *findValue(Value *V, bool OffsetOk) const; |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 99 | Value *findValueImpl(Value *V, bool OffsetOk, |
| 100 | SmallPtrSet<Value *, 4> &Visited) const; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 101 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 102 | public: |
| 103 | Module *Mod; |
| 104 | AliasAnalysis *AA; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 105 | DominatorTree *DT; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 106 | TargetData *TD; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 107 | TargetLibraryInfo *TLI; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 108 | |
| 109 | std::string Messages; |
| 110 | raw_string_ostream MessagesStr; |
| 111 | |
| 112 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 113 | Lint() : FunctionPass(ID), MessagesStr(Messages) { |
| 114 | initializeLintPass(*PassRegistry::getPassRegistry()); |
| 115 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 116 | |
| 117 | virtual bool runOnFunction(Function &F); |
| 118 | |
| 119 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 120 | AU.setPreservesAll(); |
| 121 | AU.addRequired<AliasAnalysis>(); |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 122 | AU.addRequired<TargetLibraryInfo>(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 123 | AU.addRequired<DominatorTree>(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 124 | } |
| 125 | virtual void print(raw_ostream &O, const Module *M) const {} |
| 126 | |
| 127 | void WriteValue(const Value *V) { |
| 128 | if (!V) return; |
| 129 | if (isa<Instruction>(V)) { |
| 130 | MessagesStr << *V << '\n'; |
| 131 | } else { |
| 132 | WriteAsOperand(MessagesStr, V, true, Mod); |
| 133 | MessagesStr << '\n'; |
| 134 | } |
| 135 | } |
| 136 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 137 | // CheckFailed - A check failed, so print out the condition and the message |
| 138 | // that failed. This provides a nice place to put a breakpoint if you want |
| 139 | // to see why something is not correct. |
| 140 | void CheckFailed(const Twine &Message, |
| 141 | const Value *V1 = 0, const Value *V2 = 0, |
| 142 | const Value *V3 = 0, const Value *V4 = 0) { |
| 143 | MessagesStr << Message.str() << "\n"; |
| 144 | WriteValue(V1); |
| 145 | WriteValue(V2); |
| 146 | WriteValue(V3); |
| 147 | WriteValue(V4); |
| 148 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 149 | }; |
| 150 | } |
| 151 | |
| 152 | char Lint::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 153 | INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR", |
| 154 | false, true) |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 155 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 156 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 157 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 158 | INITIALIZE_PASS_END(Lint, "lint", "Statically lint-checks LLVM IR", |
| 159 | false, true) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 160 | |
| 161 | // Assert - We know that cond should be true, if not print an error message. |
| 162 | #define Assert(C, M) \ |
| 163 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
| 164 | #define Assert1(C, M, V1) \ |
| 165 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
| 166 | #define Assert2(C, M, V1, V2) \ |
| 167 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
| 168 | #define Assert3(C, M, V1, V2, V3) \ |
| 169 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 170 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 171 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
| 172 | |
| 173 | // Lint::run - This is the main Analysis entry point for a |
| 174 | // function. |
| 175 | // |
| 176 | bool Lint::runOnFunction(Function &F) { |
| 177 | Mod = F.getParent(); |
| 178 | AA = &getAnalysis<AliasAnalysis>(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 179 | DT = &getAnalysis<DominatorTree>(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 180 | TD = getAnalysisIfAvailable<TargetData>(); |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 181 | TLI = &getAnalysis<TargetLibraryInfo>(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 182 | visit(F); |
| 183 | dbgs() << MessagesStr.str(); |
Dan Gohman | a0f7ff3 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 184 | Messages.clear(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 188 | void Lint::visitFunction(Function &F) { |
| 189 | // This isn't undefined behavior, it's just a little unusual, and it's a |
| 190 | // fairly common mistake to neglect to name a function. |
| 191 | Assert1(F.hasName() || F.hasLocalLinkage(), |
| 192 | "Unusual: Unnamed function with non-local linkage", &F); |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 193 | |
| 194 | // TODO: Check for irreducible control flow. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void Lint::visitCallSite(CallSite CS) { |
| 198 | Instruction &I = *CS.getInstruction(); |
| 199 | Value *Callee = CS.getCalledValue(); |
| 200 | |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 201 | visitMemoryReference(I, Callee, AliasAnalysis::UnknownSize, |
| 202 | 0, 0, MemRef::Callee); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 203 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 204 | if (Function *F = dyn_cast<Function>(findValue(Callee, /*OffsetOk=*/false))) { |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 205 | Assert1(CS.getCallingConv() == F->getCallingConv(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 206 | "Undefined behavior: Caller and callee calling convention differ", |
| 207 | &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 208 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 209 | FunctionType *FT = F->getFunctionType(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 210 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 211 | |
| 212 | Assert1(FT->isVarArg() ? |
| 213 | FT->getNumParams() <= NumActualArgs : |
| 214 | FT->getNumParams() == NumActualArgs, |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 215 | "Undefined behavior: Call argument count mismatches callee " |
| 216 | "argument count", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 217 | |
Dan Gohman | 545d006 | 2010-07-12 18:02:04 +0000 | [diff] [blame] | 218 | Assert1(FT->getReturnType() == I.getType(), |
| 219 | "Undefined behavior: Call return type mismatches " |
| 220 | "callee return type", &I); |
| 221 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 222 | // Check argument types (in case the callee was casted) and attributes. |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 223 | // TODO: Verify that caller and callee attributes are compatible. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 224 | Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end(); |
| 225 | CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 226 | for (; AI != AE; ++AI) { |
| 227 | Value *Actual = *AI; |
| 228 | if (PI != PE) { |
| 229 | Argument *Formal = PI++; |
| 230 | Assert1(Formal->getType() == Actual->getType(), |
| 231 | "Undefined behavior: Call argument type mismatches " |
| 232 | "callee parameter type", &I); |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 233 | |
Dan Gohman | c1f1efd | 2010-12-13 22:53:18 +0000 | [diff] [blame] | 234 | // Check that noalias arguments don't alias other arguments. This is |
| 235 | // not fully precise because we don't know the sizes of the dereferenced |
| 236 | // memory regions. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 237 | if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) |
Dan Gohman | f3b8c76 | 2010-11-11 19:23:51 +0000 | [diff] [blame] | 238 | for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI) |
Dan Gohman | 4a2a3ea | 2010-12-10 20:04:06 +0000 | [diff] [blame] | 239 | if (AI != BI && (*BI)->getType()->isPointerTy()) { |
| 240 | AliasAnalysis::AliasResult Result = AA->alias(*AI, *BI); |
| 241 | Assert1(Result != AliasAnalysis::MustAlias && |
| 242 | Result != AliasAnalysis::PartialAlias, |
| 243 | "Unusual: noalias argument aliases another argument", &I); |
| 244 | } |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 245 | |
| 246 | // Check that an sret argument points to valid memory. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 247 | if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 248 | Type *Ty = |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 249 | cast<PointerType>(Formal->getType())->getElementType(); |
| 250 | visitMemoryReference(I, Actual, AA->getTypeStoreSize(Ty), |
| 251 | TD ? TD->getABITypeAlignment(Ty) : 0, |
| 252 | Ty, MemRef::Read | MemRef::Write); |
| 253 | } |
| 254 | } |
| 255 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 258 | if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall()) |
| 259 | for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 260 | AI != AE; ++AI) { |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 261 | Value *Obj = findValue(*AI, /*OffsetOk=*/true); |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 262 | Assert1(!isa<AllocaInst>(Obj), |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 263 | "Undefined behavior: Call with \"tail\" keyword references " |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 264 | "alloca", &I); |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 267 | |
| 268 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) |
| 269 | switch (II->getIntrinsicID()) { |
| 270 | default: break; |
| 271 | |
| 272 | // TODO: Check more intrinsics |
| 273 | |
| 274 | case Intrinsic::memcpy: { |
| 275 | MemCpyInst *MCI = cast<MemCpyInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 276 | // TODO: If the size is known, use it. |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 277 | visitMemoryReference(I, MCI->getDest(), AliasAnalysis::UnknownSize, |
| 278 | MCI->getAlignment(), 0, |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 279 | MemRef::Write); |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 280 | visitMemoryReference(I, MCI->getSource(), AliasAnalysis::UnknownSize, |
| 281 | MCI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 282 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 283 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 284 | // Check that the memcpy arguments don't overlap. The AliasAnalysis API |
| 285 | // isn't expressive enough for what we really want to do. Known partial |
| 286 | // overlap is not distinguished from the case where nothing is known. |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 287 | uint64_t Size = 0; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 288 | if (const ConstantInt *Len = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 289 | dyn_cast<ConstantInt>(findValue(MCI->getLength(), |
| 290 | /*OffsetOk=*/false))) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 291 | if (Len->getValue().isIntN(32)) |
| 292 | Size = Len->getValue().getZExtValue(); |
| 293 | Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) != |
| 294 | AliasAnalysis::MustAlias, |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 295 | "Undefined behavior: memcpy source and destination overlap", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | case Intrinsic::memmove: { |
| 299 | MemMoveInst *MMI = cast<MemMoveInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 300 | // TODO: If the size is known, use it. |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 301 | visitMemoryReference(I, MMI->getDest(), AliasAnalysis::UnknownSize, |
| 302 | MMI->getAlignment(), 0, |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 303 | MemRef::Write); |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 304 | visitMemoryReference(I, MMI->getSource(), AliasAnalysis::UnknownSize, |
| 305 | MMI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 306 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | case Intrinsic::memset: { |
| 310 | MemSetInst *MSI = cast<MemSetInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 311 | // TODO: If the size is known, use it. |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 312 | visitMemoryReference(I, MSI->getDest(), AliasAnalysis::UnknownSize, |
| 313 | MSI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 314 | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | |
| 318 | case Intrinsic::vastart: |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 319 | Assert1(I.getParent()->getParent()->isVarArg(), |
| 320 | "Undefined behavior: va_start called in a non-varargs function", |
| 321 | &I); |
| 322 | |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 323 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
| 324 | 0, 0, MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 325 | break; |
| 326 | case Intrinsic::vacopy: |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 327 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
| 328 | 0, 0, MemRef::Write); |
| 329 | visitMemoryReference(I, CS.getArgument(1), AliasAnalysis::UnknownSize, |
| 330 | 0, 0, MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 331 | break; |
| 332 | case Intrinsic::vaend: |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 333 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
| 334 | 0, 0, MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 335 | break; |
Dan Gohman | 882ddb4 | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 336 | |
| 337 | case Intrinsic::stackrestore: |
| 338 | // Stackrestore doesn't read or write memory, but it sets the |
| 339 | // stack pointer, which the compiler may read from or write to |
| 340 | // at any time, so check it for both readability and writeability. |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 341 | visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize, |
| 342 | 0, 0, MemRef::Read | MemRef::Write); |
Dan Gohman | 882ddb4 | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 343 | break; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | void Lint::visitCallInst(CallInst &I) { |
| 348 | return visitCallSite(&I); |
| 349 | } |
| 350 | |
| 351 | void Lint::visitInvokeInst(InvokeInst &I) { |
| 352 | return visitCallSite(&I); |
| 353 | } |
| 354 | |
| 355 | void Lint::visitReturnInst(ReturnInst &I) { |
| 356 | Function *F = I.getParent()->getParent(); |
| 357 | Assert1(!F->doesNotReturn(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 358 | "Unusual: Return statement in function with noreturn attribute", |
| 359 | &I); |
Dan Gohman | 292fc87 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 360 | |
| 361 | if (Value *V = I.getReturnValue()) { |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 362 | Value *Obj = findValue(V, /*OffsetOk=*/true); |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 363 | Assert1(!isa<AllocaInst>(Obj), |
| 364 | "Unusual: Returning alloca value", &I); |
Dan Gohman | 292fc87 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 365 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 368 | // TODO: Check that the reference is in bounds. |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 369 | // TODO: Check readnone/readonly function attributes. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 370 | void Lint::visitMemoryReference(Instruction &I, |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 371 | Value *Ptr, uint64_t Size, unsigned Align, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 372 | Type *Ty, unsigned Flags) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 373 | // If no memory is being referenced, it doesn't matter if the pointer |
| 374 | // is valid. |
| 375 | if (Size == 0) |
| 376 | return; |
| 377 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 378 | Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 379 | Assert1(!isa<ConstantPointerNull>(UnderlyingObject), |
| 380 | "Undefined behavior: Null pointer dereference", &I); |
| 381 | Assert1(!isa<UndefValue>(UnderlyingObject), |
| 382 | "Undefined behavior: Undef pointer dereference", &I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 383 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 384 | !cast<ConstantInt>(UnderlyingObject)->isAllOnesValue(), |
| 385 | "Unusual: All-ones pointer dereference", &I); |
| 386 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 387 | !cast<ConstantInt>(UnderlyingObject)->isOne(), |
| 388 | "Unusual: Address one pointer dereference", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 389 | |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 390 | if (Flags & MemRef::Write) { |
| 391 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject)) |
| 392 | Assert1(!GV->isConstant(), |
| 393 | "Undefined behavior: Write to read-only memory", &I); |
| 394 | Assert1(!isa<Function>(UnderlyingObject) && |
| 395 | !isa<BlockAddress>(UnderlyingObject), |
| 396 | "Undefined behavior: Write to text section", &I); |
| 397 | } |
| 398 | if (Flags & MemRef::Read) { |
| 399 | Assert1(!isa<Function>(UnderlyingObject), |
| 400 | "Unusual: Load from function body", &I); |
| 401 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 402 | "Undefined behavior: Load from block address", &I); |
| 403 | } |
| 404 | if (Flags & MemRef::Callee) { |
| 405 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 406 | "Undefined behavior: Call to block address", &I); |
| 407 | } |
| 408 | if (Flags & MemRef::Branchee) { |
| 409 | Assert1(!isa<Constant>(UnderlyingObject) || |
| 410 | isa<BlockAddress>(UnderlyingObject), |
| 411 | "Undefined behavior: Branch to non-blockaddress", &I); |
| 412 | } |
| 413 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 414 | if (TD) { |
| 415 | if (Align == 0 && Ty) Align = TD->getABITypeAlignment(Ty); |
| 416 | |
| 417 | if (Align != 0) { |
| 418 | unsigned BitWidth = TD->getTypeSizeInBits(Ptr->getType()); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 419 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 420 | ComputeMaskedBits(Ptr, KnownZero, KnownOne, TD); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 421 | Assert1(!(KnownOne & APInt::getLowBitsSet(BitWidth, Log2_32(Align))), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 422 | "Undefined behavior: Memory reference address is misaligned", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void Lint::visitLoadInst(LoadInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 428 | visitMemoryReference(I, I.getPointerOperand(), |
| 429 | AA->getTypeStoreSize(I.getType()), I.getAlignment(), |
| 430 | I.getType(), MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void Lint::visitStoreInst(StoreInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 434 | visitMemoryReference(I, I.getPointerOperand(), |
| 435 | AA->getTypeStoreSize(I.getOperand(0)->getType()), |
| 436 | I.getAlignment(), |
| 437 | I.getOperand(0)->getType(), MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 440 | void Lint::visitXor(BinaryOperator &I) { |
| 441 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 442 | !isa<UndefValue>(I.getOperand(1)), |
| 443 | "Undefined result: xor(undef, undef)", &I); |
| 444 | } |
| 445 | |
| 446 | void Lint::visitSub(BinaryOperator &I) { |
| 447 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 448 | !isa<UndefValue>(I.getOperand(1)), |
| 449 | "Undefined result: sub(undef, undef)", &I); |
| 450 | } |
| 451 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 452 | void Lint::visitLShr(BinaryOperator &I) { |
| 453 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 454 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 455 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 456 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void Lint::visitAShr(BinaryOperator &I) { |
| 460 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 461 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 462 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 463 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void Lint::visitShl(BinaryOperator &I) { |
| 467 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 468 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 469 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 470 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 473 | static bool isZero(Value *V, TargetData *TD) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 474 | // Assume undef could be zero. |
| 475 | if (isa<UndefValue>(V)) return true; |
| 476 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 477 | unsigned BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 478 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 479 | ComputeMaskedBits(V, KnownZero, KnownOne, TD); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 480 | return KnownZero.isAllOnesValue(); |
| 481 | } |
| 482 | |
| 483 | void Lint::visitSDiv(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 484 | Assert1(!isZero(I.getOperand(1), TD), |
| 485 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void Lint::visitUDiv(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 489 | Assert1(!isZero(I.getOperand(1), TD), |
| 490 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void Lint::visitSRem(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 494 | Assert1(!isZero(I.getOperand(1), TD), |
| 495 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void Lint::visitURem(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 499 | Assert1(!isZero(I.getOperand(1), TD), |
| 500 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | void Lint::visitAllocaInst(AllocaInst &I) { |
| 504 | if (isa<ConstantInt>(I.getArraySize())) |
| 505 | // This isn't undefined behavior, it's just an obvious pessimization. |
| 506 | Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 507 | "Pessimization: Static alloca outside of entry block", &I); |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 508 | |
| 509 | // TODO: Check for an unusual size (MSB set?) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void Lint::visitVAArgInst(VAArgInst &I) { |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 513 | visitMemoryReference(I, I.getOperand(0), AliasAnalysis::UnknownSize, 0, 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 514 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void Lint::visitIndirectBrInst(IndirectBrInst &I) { |
Dan Gohman | f3a925d | 2010-10-19 17:06:23 +0000 | [diff] [blame] | 518 | visitMemoryReference(I, I.getAddress(), AliasAnalysis::UnknownSize, 0, 0, |
| 519 | MemRef::Branchee); |
Dan Gohman | a8afb2a | 2010-08-02 23:06:43 +0000 | [diff] [blame] | 520 | |
| 521 | Assert1(I.getNumDestinations() != 0, |
| 522 | "Undefined behavior: indirectbr with no destinations", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 525 | void Lint::visitExtractElementInst(ExtractElementInst &I) { |
| 526 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 527 | dyn_cast<ConstantInt>(findValue(I.getIndexOperand(), |
| 528 | /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 529 | Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 530 | "Undefined result: extractelement index out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void Lint::visitInsertElementInst(InsertElementInst &I) { |
| 534 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 535 | dyn_cast<ConstantInt>(findValue(I.getOperand(2), |
| 536 | /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 537 | Assert1(CI->getValue().ult(I.getType()->getNumElements()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 538 | "Undefined result: insertelement index out of range", &I); |
| 539 | } |
| 540 | |
| 541 | void Lint::visitUnreachableInst(UnreachableInst &I) { |
| 542 | // This isn't undefined behavior, it's merely suspicious. |
| 543 | Assert1(&I == I.getParent()->begin() || |
| 544 | prior(BasicBlock::iterator(&I))->mayHaveSideEffects(), |
| 545 | "Unusual: unreachable immediately preceded by instruction without " |
| 546 | "side effects", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 549 | /// findValue - Look through bitcasts and simple memory reference patterns |
| 550 | /// to identify an equivalent, but more informative, value. If OffsetOk |
| 551 | /// is true, look through getelementptrs with non-zero offsets too. |
| 552 | /// |
| 553 | /// Most analysis passes don't require this logic, because instcombine |
| 554 | /// will simplify most of these kinds of things away. But it's a goal of |
| 555 | /// this Lint pass to be useful even on non-optimized IR. |
| 556 | Value *Lint::findValue(Value *V, bool OffsetOk) const { |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 557 | SmallPtrSet<Value *, 4> Visited; |
| 558 | return findValueImpl(V, OffsetOk, Visited); |
| 559 | } |
| 560 | |
| 561 | /// findValueImpl - Implementation helper for findValue. |
| 562 | Value *Lint::findValueImpl(Value *V, bool OffsetOk, |
| 563 | SmallPtrSet<Value *, 4> &Visited) const { |
| 564 | // Detect self-referential values. |
| 565 | if (!Visited.insert(V)) |
| 566 | return UndefValue::get(V->getType()); |
| 567 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 568 | // TODO: Look through sext or zext cast, when the result is known to |
| 569 | // be interpreted as signed or unsigned, respectively. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 570 | // TODO: Look through eliminable cast pairs. |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 571 | // TODO: Look through calls with unique return values. |
| 572 | // TODO: Look through vector insert/extract/shuffle. |
Dan Gohman | bd1801b | 2011-01-24 18:53:32 +0000 | [diff] [blame] | 573 | V = OffsetOk ? GetUnderlyingObject(V, TD) : V->stripPointerCasts(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 574 | if (LoadInst *L = dyn_cast<LoadInst>(V)) { |
| 575 | BasicBlock::iterator BBI = L; |
| 576 | BasicBlock *BB = L->getParent(); |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 577 | SmallPtrSet<BasicBlock *, 4> VisitedBlocks; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 578 | for (;;) { |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 579 | if (!VisitedBlocks.insert(BB)) break; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 580 | if (Value *U = FindAvailableLoadedValue(L->getPointerOperand(), |
| 581 | BB, BBI, 6, AA)) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 582 | return findValueImpl(U, OffsetOk, Visited); |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 583 | if (BBI != BB->begin()) break; |
| 584 | BB = BB->getUniquePredecessor(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 585 | if (!BB) break; |
| 586 | BBI = BB->end(); |
| 587 | } |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 588 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 589 | if (Value *W = PN->hasConstantValue()) |
Duncan Sands | 23a1957 | 2010-11-17 10:23:23 +0000 | [diff] [blame] | 590 | if (W != V) |
| 591 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 592 | } else if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 593 | if (CI->isNoopCast(TD ? TD->getIntPtrType(V->getContext()) : |
| 594 | Type::getInt64Ty(V->getContext()))) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 595 | return findValueImpl(CI->getOperand(0), OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 596 | } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) { |
| 597 | if (Value *W = FindInsertedValue(Ex->getAggregateOperand(), |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 598 | Ex->getIndices())) |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 599 | if (W != V) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 600 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 601 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 602 | // Same as above, but for ConstantExpr instead of Instruction. |
| 603 | if (Instruction::isCast(CE->getOpcode())) { |
| 604 | if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()), |
| 605 | CE->getOperand(0)->getType(), |
| 606 | CE->getType(), |
| 607 | TD ? TD->getIntPtrType(V->getContext()) : |
| 608 | Type::getInt64Ty(V->getContext()))) |
| 609 | return findValueImpl(CE->getOperand(0), OffsetOk, Visited); |
| 610 | } else if (CE->getOpcode() == Instruction::ExtractValue) { |
Jay Foad | d30aa5a | 2011-04-13 15:22:40 +0000 | [diff] [blame] | 611 | ArrayRef<unsigned> Indices = CE->getIndices(); |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 612 | if (Value *W = FindInsertedValue(CE->getOperand(0), Indices)) |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 613 | if (W != V) |
| 614 | return findValueImpl(W, OffsetOk, Visited); |
| 615 | } |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | // As a last resort, try SimplifyInstruction or constant folding. |
| 619 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 620 | if (Value *W = SimplifyInstruction(Inst, TD, TLI, DT)) |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 621 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 622 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 623 | if (Value *W = ConstantFoldConstantExpression(CE, TD, TLI)) |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 624 | if (W != V) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 625 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return V; |
| 629 | } |
| 630 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 631 | //===----------------------------------------------------------------------===// |
| 632 | // Implement the public interfaces to this file... |
| 633 | //===----------------------------------------------------------------------===// |
| 634 | |
| 635 | FunctionPass *llvm::createLintPass() { |
| 636 | return new Lint(); |
| 637 | } |
| 638 | |
| 639 | /// lintFunction - Check a function for errors, printing messages on stderr. |
| 640 | /// |
| 641 | void llvm::lintFunction(const Function &f) { |
| 642 | Function &F = const_cast<Function&>(f); |
| 643 | assert(!F.isDeclaration() && "Cannot lint external functions"); |
| 644 | |
| 645 | FunctionPassManager FPM(F.getParent()); |
| 646 | Lint *V = new Lint(); |
| 647 | FPM.add(V); |
| 648 | FPM.run(F); |
| 649 | } |
| 650 | |
| 651 | /// lintModule - Check a module for errors, printing messages on stderr. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 652 | /// |
Dan Gohman | a0f7ff3 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 653 | void llvm::lintModule(const Module &M) { |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 654 | PassManager PM; |
| 655 | Lint *V = new Lint(); |
| 656 | PM.add(V); |
| 657 | PM.run(const_cast<Module&>(M)); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 658 | } |