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