blob: 48ea8885e310cb4ce62386e61748963d3d07d5bb [file] [log] [blame]
Dan Gohman98bc4372010-04-08 18:47:09 +00001//===-- 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 Arsenaulta236ea52014-03-06 17:33:55 +000019//
Dan Gohman98bc4372010-04-08 18:47:09 +000020// 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 Gohmanf855b392010-07-06 15:21:57 +000022// 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 Gohmanc951e6e2010-04-22 01:30:05 +000024//
Dan Gohman98bc4372010-04-08 18:47:09 +000025// 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 Arsenaulta236ea52014-03-06 17:33:55 +000029//
Dan Gohman98bc4372010-04-08 18:47:09 +000030// 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 Arsenaulta236ea52014-03-06 17:33:55 +000034//
Dan Gohman98bc4372010-04-08 18:47:09 +000035//===----------------------------------------------------------------------===//
36
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/Analysis/Lint.h"
38#include "llvm/ADT/STLExtras.h"
Dan Gohman98bc4372010-04-08 18:47:09 +000039#include "llvm/Analysis/AliasAnalysis.h"
Hal Finkel60db0582014-09-07 18:57:58 +000040#include "llvm/Analysis/AssumptionTracker.h"
Dan Gohman54d7aaa2010-05-28 16:21:24 +000041#include "llvm/Analysis/ConstantFolding.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Analysis/InstructionSimplify.h"
Dan Gohman54d7aaa2010-05-28 16:21:24 +000043#include "llvm/Analysis/Loads.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/Analysis/Passes.h"
Dan Gohman98bc4372010-04-08 18:47:09 +000045#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000046#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000048#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000049#include "llvm/IR/Function.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000050#include "llvm/IR/InstVisitor.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000051#include "llvm/IR/IntrinsicInst.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000052#include "llvm/Pass.h"
53#include "llvm/PassManager.h"
Dan Gohman98bc4372010-04-08 18:47:09 +000054#include "llvm/Support/Debug.h"
Dan Gohman98bc4372010-04-08 18:47:09 +000055#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000056#include "llvm/Target/TargetLibraryInfo.h"
Dan Gohman98bc4372010-04-08 18:47:09 +000057using namespace llvm;
58
59namespace {
Dan Gohman299e7b92010-04-30 19:05:00 +000060 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 Gohman98bc4372010-04-08 18:47:09 +000067 class Lint : public FunctionPass, public InstVisitor<Lint> {
68 friend class InstVisitor<Lint>;
69
Dan Gohman9ba08a42010-04-09 01:39:53 +000070 void visitFunction(Function &F);
71
Dan Gohman98bc4372010-04-08 18:47:09 +000072 void visitCallSite(CallSite CS);
Dan Gohman0fa67e42010-05-28 21:43:57 +000073 void visitMemoryReference(Instruction &I, Value *Ptr,
Dan Gohmanf372cf82010-10-19 22:54:46 +000074 uint64_t Size, unsigned Align,
Chris Lattner229907c2011-07-18 04:54:35 +000075 Type *Ty, unsigned Flags);
Dan Gohman98bc4372010-04-08 18:47:09 +000076
Dan Gohman98bc4372010-04-08 18:47:09 +000077 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 Gohman9ba08a42010-04-09 01:39:53 +000082 void visitXor(BinaryOperator &I);
83 void visitSub(BinaryOperator &I);
Dan Gohman7808d492010-04-08 23:05:57 +000084 void visitLShr(BinaryOperator &I);
85 void visitAShr(BinaryOperator &I);
86 void visitShl(BinaryOperator &I);
Dan Gohman98bc4372010-04-08 18:47:09 +000087 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 Gohman7808d492010-04-08 23:05:57 +000094 void visitExtractElementInst(ExtractElementInst &I);
95 void visitInsertElementInst(InsertElementInst &I);
Dan Gohman9ba08a42010-04-09 01:39:53 +000096 void visitUnreachableInst(UnreachableInst &I);
Dan Gohman98bc4372010-04-08 18:47:09 +000097
Dan Gohman54d7aaa2010-05-28 16:21:24 +000098 Value *findValue(Value *V, bool OffsetOk) const;
Dan Gohman862f0342010-05-28 16:45:33 +000099 Value *findValueImpl(Value *V, bool OffsetOk,
Craig Topper71b7b682014-08-21 05:55:13 +0000100 SmallPtrSetImpl<Value *> &Visited) const;
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000101
Dan Gohman98bc4372010-04-08 18:47:09 +0000102 public:
103 Module *Mod;
104 AliasAnalysis *AA;
Hal Finkel60db0582014-09-07 18:57:58 +0000105 AssumptionTracker *AT;
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000106 DominatorTree *DT;
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000107 const DataLayout *DL;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000108 TargetLibraryInfo *TLI;
Dan Gohman98bc4372010-04-08 18:47:09 +0000109
Alp Tokere69170a2014-06-26 22:52:05 +0000110 std::string Messages;
111 raw_string_ostream MessagesStr;
Dan Gohman98bc4372010-04-08 18:47:09 +0000112
113 static char ID; // Pass identification, replacement for typeid
Alp Tokere69170a2014-06-26 22:52:05 +0000114 Lint() : FunctionPass(ID), MessagesStr(Messages) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000115 initializeLintPass(*PassRegistry::getPassRegistry());
116 }
Dan Gohman98bc4372010-04-08 18:47:09 +0000117
Craig Toppere9ba7592014-03-05 07:30:04 +0000118 bool runOnFunction(Function &F) override;
Dan Gohman98bc4372010-04-08 18:47:09 +0000119
Craig Toppere9ba7592014-03-05 07:30:04 +0000120 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohman98bc4372010-04-08 18:47:09 +0000121 AU.setPreservesAll();
122 AU.addRequired<AliasAnalysis>();
Hal Finkel60db0582014-09-07 18:57:58 +0000123 AU.addRequired<AssumptionTracker>();
Chad Rosierc24b86f2011-12-01 03:08:23 +0000124 AU.addRequired<TargetLibraryInfo>();
Chandler Carruth73523022014-01-13 13:07:17 +0000125 AU.addRequired<DominatorTreeWrapperPass>();
Dan Gohman98bc4372010-04-08 18:47:09 +0000126 }
Craig Toppere9ba7592014-03-05 07:30:04 +0000127 void print(raw_ostream &O, const Module *M) const override {}
Dan Gohman98bc4372010-04-08 18:47:09 +0000128
129 void WriteValue(const Value *V) {
130 if (!V) return;
131 if (isa<Instruction>(V)) {
132 MessagesStr << *V << '\n';
133 } else {
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000134 V->printAsOperand(MessagesStr, true, Mod);
Dan Gohman98bc4372010-04-08 18:47:09 +0000135 MessagesStr << '\n';
136 }
137 }
138
Dan Gohman98bc4372010-04-08 18:47:09 +0000139 // CheckFailed - A check failed, so print out the condition and the message
140 // that failed. This provides a nice place to put a breakpoint if you want
141 // to see why something is not correct.
142 void CheckFailed(const Twine &Message,
Craig Topper9f008862014-04-15 04:59:12 +0000143 const Value *V1 = nullptr, const Value *V2 = nullptr,
144 const Value *V3 = nullptr, const Value *V4 = nullptr) {
Dan Gohman98bc4372010-04-08 18:47:09 +0000145 MessagesStr << Message.str() << "\n";
146 WriteValue(V1);
147 WriteValue(V2);
148 WriteValue(V3);
149 WriteValue(V4);
150 }
Dan Gohman98bc4372010-04-08 18:47:09 +0000151 };
152}
153
154char Lint::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000155INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR",
156 false, true)
Hal Finkel60db0582014-09-07 18:57:58 +0000157INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000158INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Chandler Carruth73523022014-01-13 13:07:17 +0000159INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000160INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
161INITIALIZE_PASS_END(Lint, "lint", "Statically lint-checks LLVM IR",
162 false, true)
Dan Gohman98bc4372010-04-08 18:47:09 +0000163
164// Assert - We know that cond should be true, if not print an error message.
165#define Assert(C, M) \
166 do { if (!(C)) { CheckFailed(M); return; } } while (0)
167#define Assert1(C, M, V1) \
168 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
169#define Assert2(C, M, V1, V2) \
170 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
171#define Assert3(C, M, V1, V2, V3) \
172 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
173#define Assert4(C, M, V1, V2, V3, V4) \
174 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
175
176// Lint::run - This is the main Analysis entry point for a
177// function.
178//
179bool Lint::runOnFunction(Function &F) {
180 Mod = F.getParent();
181 AA = &getAnalysis<AliasAnalysis>();
Hal Finkel60db0582014-09-07 18:57:58 +0000182 AT = &getAnalysis<AssumptionTracker>();
Chandler Carruth73523022014-01-13 13:07:17 +0000183 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Rafael Espindola93512512014-02-25 17:30:31 +0000184 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topper9f008862014-04-15 04:59:12 +0000185 DL = DLP ? &DLP->getDataLayout() : nullptr;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000186 TLI = &getAnalysis<TargetLibraryInfo>();
Dan Gohman98bc4372010-04-08 18:47:09 +0000187 visit(F);
188 dbgs() << MessagesStr.str();
Alp Tokere69170a2014-06-26 22:52:05 +0000189 Messages.clear();
Dan Gohman98bc4372010-04-08 18:47:09 +0000190 return false;
191}
192
Dan Gohman9ba08a42010-04-09 01:39:53 +0000193void Lint::visitFunction(Function &F) {
194 // This isn't undefined behavior, it's just a little unusual, and it's a
195 // fairly common mistake to neglect to name a function.
196 Assert1(F.hasName() || F.hasLocalLinkage(),
197 "Unusual: Unnamed function with non-local linkage", &F);
Dan Gohman1e33b182010-07-06 15:23:00 +0000198
199 // TODO: Check for irreducible control flow.
Dan Gohman98bc4372010-04-08 18:47:09 +0000200}
201
202void Lint::visitCallSite(CallSite CS) {
203 Instruction &I = *CS.getInstruction();
204 Value *Callee = CS.getCalledValue();
205
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000206 visitMemoryReference(I, Callee, AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000207 0, nullptr, MemRef::Callee);
Dan Gohman98bc4372010-04-08 18:47:09 +0000208
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000209 if (Function *F = dyn_cast<Function>(findValue(Callee, /*OffsetOk=*/false))) {
Dan Gohman98bc4372010-04-08 18:47:09 +0000210 Assert1(CS.getCallingConv() == F->getCallingConv(),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000211 "Undefined behavior: Caller and callee calling convention differ",
212 &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000213
Chris Lattner229907c2011-07-18 04:54:35 +0000214 FunctionType *FT = F->getFunctionType();
Matt Arsenaultb12f2f32013-11-10 03:18:50 +0000215 unsigned NumActualArgs = CS.arg_size();
Dan Gohman98bc4372010-04-08 18:47:09 +0000216
217 Assert1(FT->isVarArg() ?
218 FT->getNumParams() <= NumActualArgs :
219 FT->getNumParams() == NumActualArgs,
Dan Gohman9ba08a42010-04-09 01:39:53 +0000220 "Undefined behavior: Call argument count mismatches callee "
221 "argument count", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000222
Dan Gohmanc128e702010-07-12 18:02:04 +0000223 Assert1(FT->getReturnType() == I.getType(),
224 "Undefined behavior: Call return type mismatches "
225 "callee return type", &I);
226
Dan Gohman0fa67e42010-05-28 21:43:57 +0000227 // Check argument types (in case the callee was casted) and attributes.
Dan Gohman1e33b182010-07-06 15:23:00 +0000228 // TODO: Verify that caller and callee attributes are compatible.
Dan Gohman0fa67e42010-05-28 21:43:57 +0000229 Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end();
230 CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
231 for (; AI != AE; ++AI) {
232 Value *Actual = *AI;
233 if (PI != PE) {
234 Argument *Formal = PI++;
235 Assert1(Formal->getType() == Actual->getType(),
236 "Undefined behavior: Call argument type mismatches "
237 "callee parameter type", &I);
Dan Gohman49a372c2010-06-01 20:51:40 +0000238
Dan Gohman3cb55a12010-12-13 22:53:18 +0000239 // Check that noalias arguments don't alias other arguments. This is
240 // not fully precise because we don't know the sizes of the dereferenced
241 // memory regions.
Dan Gohman0fa67e42010-05-28 21:43:57 +0000242 if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy())
Dan Gohman7dacf8f2010-11-11 19:23:51 +0000243 for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI)
Dan Gohman201acdb2010-12-10 20:04:06 +0000244 if (AI != BI && (*BI)->getType()->isPointerTy()) {
245 AliasAnalysis::AliasResult Result = AA->alias(*AI, *BI);
246 Assert1(Result != AliasAnalysis::MustAlias &&
247 Result != AliasAnalysis::PartialAlias,
248 "Unusual: noalias argument aliases another argument", &I);
249 }
Dan Gohman49a372c2010-06-01 20:51:40 +0000250
251 // Check that an sret argument points to valid memory.
Dan Gohman0fa67e42010-05-28 21:43:57 +0000252 if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) {
Chris Lattner229907c2011-07-18 04:54:35 +0000253 Type *Ty =
Dan Gohman0fa67e42010-05-28 21:43:57 +0000254 cast<PointerType>(Formal->getType())->getElementType();
255 visitMemoryReference(I, Actual, AA->getTypeStoreSize(Ty),
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000256 DL ? DL->getABITypeAlignment(Ty) : 0,
Dan Gohman0fa67e42010-05-28 21:43:57 +0000257 Ty, MemRef::Read | MemRef::Write);
258 }
259 }
260 }
Dan Gohman98bc4372010-04-08 18:47:09 +0000261 }
262
Dan Gohman1249adf2010-05-26 21:46:36 +0000263 if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall())
264 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
265 AI != AE; ++AI) {
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000266 Value *Obj = findValue(*AI, /*OffsetOk=*/true);
Dan Gohmancef9fc32010-05-28 16:34:49 +0000267 Assert1(!isa<AllocaInst>(Obj),
Dan Gohman1249adf2010-05-26 21:46:36 +0000268 "Undefined behavior: Call with \"tail\" keyword references "
Dan Gohmancef9fc32010-05-28 16:34:49 +0000269 "alloca", &I);
Dan Gohman1249adf2010-05-26 21:46:36 +0000270 }
271
Dan Gohman98bc4372010-04-08 18:47:09 +0000272
273 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I))
274 switch (II->getIntrinsicID()) {
275 default: break;
276
277 // TODO: Check more intrinsics
278
279 case Intrinsic::memcpy: {
280 MemCpyInst *MCI = cast<MemCpyInst>(&I);
Dan Gohman0fa67e42010-05-28 21:43:57 +0000281 // TODO: If the size is known, use it.
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000282 visitMemoryReference(I, MCI->getDest(), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000283 MCI->getAlignment(), nullptr,
Dan Gohmanc575ec62010-05-28 17:44:00 +0000284 MemRef::Write);
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000285 visitMemoryReference(I, MCI->getSource(), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000286 MCI->getAlignment(), nullptr,
Dan Gohman299e7b92010-04-30 19:05:00 +0000287 MemRef::Read);
Dan Gohman98bc4372010-04-08 18:47:09 +0000288
Dan Gohman9ba08a42010-04-09 01:39:53 +0000289 // Check that the memcpy arguments don't overlap. The AliasAnalysis API
290 // isn't expressive enough for what we really want to do. Known partial
291 // overlap is not distinguished from the case where nothing is known.
Dan Gohmanf372cf82010-10-19 22:54:46 +0000292 uint64_t Size = 0;
Dan Gohman98bc4372010-04-08 18:47:09 +0000293 if (const ConstantInt *Len =
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000294 dyn_cast<ConstantInt>(findValue(MCI->getLength(),
295 /*OffsetOk=*/false)))
Dan Gohman98bc4372010-04-08 18:47:09 +0000296 if (Len->getValue().isIntN(32))
297 Size = Len->getValue().getZExtValue();
298 Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) !=
299 AliasAnalysis::MustAlias,
Dan Gohman9ba08a42010-04-09 01:39:53 +0000300 "Undefined behavior: memcpy source and destination overlap", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000301 break;
302 }
303 case Intrinsic::memmove: {
304 MemMoveInst *MMI = cast<MemMoveInst>(&I);
Dan Gohman0fa67e42010-05-28 21:43:57 +0000305 // TODO: If the size is known, use it.
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000306 visitMemoryReference(I, MMI->getDest(), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000307 MMI->getAlignment(), nullptr,
Dan Gohmanc575ec62010-05-28 17:44:00 +0000308 MemRef::Write);
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000309 visitMemoryReference(I, MMI->getSource(), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000310 MMI->getAlignment(), nullptr,
Dan Gohman299e7b92010-04-30 19:05:00 +0000311 MemRef::Read);
Dan Gohman98bc4372010-04-08 18:47:09 +0000312 break;
313 }
314 case Intrinsic::memset: {
315 MemSetInst *MSI = cast<MemSetInst>(&I);
Dan Gohman0fa67e42010-05-28 21:43:57 +0000316 // TODO: If the size is known, use it.
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000317 visitMemoryReference(I, MSI->getDest(), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000318 MSI->getAlignment(), nullptr,
Dan Gohman299e7b92010-04-30 19:05:00 +0000319 MemRef::Write);
Dan Gohman98bc4372010-04-08 18:47:09 +0000320 break;
321 }
322
323 case Intrinsic::vastart:
Dan Gohman9ba08a42010-04-09 01:39:53 +0000324 Assert1(I.getParent()->getParent()->isVarArg(),
325 "Undefined behavior: va_start called in a non-varargs function",
326 &I);
327
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000328 visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000329 0, nullptr, MemRef::Read | MemRef::Write);
Dan Gohman98bc4372010-04-08 18:47:09 +0000330 break;
331 case Intrinsic::vacopy:
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000332 visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000333 0, nullptr, MemRef::Write);
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000334 visitMemoryReference(I, CS.getArgument(1), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000335 0, nullptr, MemRef::Read);
Dan Gohman98bc4372010-04-08 18:47:09 +0000336 break;
337 case Intrinsic::vaend:
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000338 visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000339 0, nullptr, MemRef::Read | MemRef::Write);
Dan Gohman98bc4372010-04-08 18:47:09 +0000340 break;
Dan Gohmana20a5cd2010-05-26 22:21:25 +0000341
342 case Intrinsic::stackrestore:
343 // Stackrestore doesn't read or write memory, but it sets the
344 // stack pointer, which the compiler may read from or write to
345 // at any time, so check it for both readability and writeability.
Dan Gohman14fe8cf22010-10-19 17:06:23 +0000346 visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize,
Craig Topper9f008862014-04-15 04:59:12 +0000347 0, nullptr, MemRef::Read | MemRef::Write);
Dan Gohmana20a5cd2010-05-26 22:21:25 +0000348 break;
Dan Gohman98bc4372010-04-08 18:47:09 +0000349 }
350}
351
352void Lint::visitCallInst(CallInst &I) {
353 return visitCallSite(&I);
354}
355
356void Lint::visitInvokeInst(InvokeInst &I) {
357 return visitCallSite(&I);
358}
359
360void Lint::visitReturnInst(ReturnInst &I) {
361 Function *F = I.getParent()->getParent();
362 Assert1(!F->doesNotReturn(),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000363 "Unusual: Return statement in function with noreturn attribute",
364 &I);
Dan Gohmanddba4b72010-05-28 04:33:42 +0000365
366 if (Value *V = I.getReturnValue()) {
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000367 Value *Obj = findValue(V, /*OffsetOk=*/true);
Dan Gohmancef9fc32010-05-28 16:34:49 +0000368 Assert1(!isa<AllocaInst>(Obj),
369 "Unusual: Returning alloca value", &I);
Dan Gohmanddba4b72010-05-28 04:33:42 +0000370 }
Dan Gohman98bc4372010-04-08 18:47:09 +0000371}
372
Dan Gohman0fa67e42010-05-28 21:43:57 +0000373// TODO: Check that the reference is in bounds.
Dan Gohman1e33b182010-07-06 15:23:00 +0000374// TODO: Check readnone/readonly function attributes.
Dan Gohman98bc4372010-04-08 18:47:09 +0000375void Lint::visitMemoryReference(Instruction &I,
Dan Gohmanf372cf82010-10-19 22:54:46 +0000376 Value *Ptr, uint64_t Size, unsigned Align,
Chris Lattner229907c2011-07-18 04:54:35 +0000377 Type *Ty, unsigned Flags) {
Dan Gohman0fa67e42010-05-28 21:43:57 +0000378 // If no memory is being referenced, it doesn't matter if the pointer
379 // is valid.
380 if (Size == 0)
381 return;
382
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000383 Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true);
Dan Gohman9ba08a42010-04-09 01:39:53 +0000384 Assert1(!isa<ConstantPointerNull>(UnderlyingObject),
385 "Undefined behavior: Null pointer dereference", &I);
386 Assert1(!isa<UndefValue>(UnderlyingObject),
387 "Undefined behavior: Undef pointer dereference", &I);
Dan Gohman0fa67e42010-05-28 21:43:57 +0000388 Assert1(!isa<ConstantInt>(UnderlyingObject) ||
389 !cast<ConstantInt>(UnderlyingObject)->isAllOnesValue(),
390 "Unusual: All-ones pointer dereference", &I);
391 Assert1(!isa<ConstantInt>(UnderlyingObject) ||
392 !cast<ConstantInt>(UnderlyingObject)->isOne(),
393 "Unusual: Address one pointer dereference", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000394
Dan Gohman299e7b92010-04-30 19:05:00 +0000395 if (Flags & MemRef::Write) {
396 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject))
397 Assert1(!GV->isConstant(),
398 "Undefined behavior: Write to read-only memory", &I);
399 Assert1(!isa<Function>(UnderlyingObject) &&
400 !isa<BlockAddress>(UnderlyingObject),
401 "Undefined behavior: Write to text section", &I);
402 }
403 if (Flags & MemRef::Read) {
404 Assert1(!isa<Function>(UnderlyingObject),
405 "Unusual: Load from function body", &I);
406 Assert1(!isa<BlockAddress>(UnderlyingObject),
407 "Undefined behavior: Load from block address", &I);
408 }
409 if (Flags & MemRef::Callee) {
410 Assert1(!isa<BlockAddress>(UnderlyingObject),
411 "Undefined behavior: Call to block address", &I);
412 }
413 if (Flags & MemRef::Branchee) {
414 Assert1(!isa<Constant>(UnderlyingObject) ||
415 isa<BlockAddress>(UnderlyingObject),
416 "Undefined behavior: Branch to non-blockaddress", &I);
417 }
418
Duncan Sandsa221eea2012-09-26 07:45:36 +0000419 // Check for buffer overflows and misalignment.
Dan Gohman20a2ae92013-01-31 02:00:45 +0000420 // Only handles memory references that read/write something simple like an
421 // alloca instruction or a global variable.
422 int64_t Offset = 0;
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000423 if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, DL)) {
Dan Gohman20a2ae92013-01-31 02:00:45 +0000424 // OK, so the access is to a constant offset from Ptr. Check that Ptr is
425 // something we can handle and if so extract the size of this base object
426 // along with its alignment.
427 uint64_t BaseSize = AliasAnalysis::UnknownSize;
428 unsigned BaseAlign = 0;
Dan Gohman98bc4372010-04-08 18:47:09 +0000429
Dan Gohman20a2ae92013-01-31 02:00:45 +0000430 if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
431 Type *ATy = AI->getAllocatedType();
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000432 if (DL && !AI->isArrayAllocation() && ATy->isSized())
433 BaseSize = DL->getTypeAllocSize(ATy);
Dan Gohman20a2ae92013-01-31 02:00:45 +0000434 BaseAlign = AI->getAlignment();
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000435 if (DL && BaseAlign == 0 && ATy->isSized())
436 BaseAlign = DL->getABITypeAlignment(ATy);
Dan Gohman20a2ae92013-01-31 02:00:45 +0000437 } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
438 // If the global may be defined differently in another compilation unit
439 // then don't warn about funky memory accesses.
440 if (GV->hasDefinitiveInitializer()) {
441 Type *GTy = GV->getType()->getElementType();
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000442 if (DL && GTy->isSized())
443 BaseSize = DL->getTypeAllocSize(GTy);
Dan Gohman20a2ae92013-01-31 02:00:45 +0000444 BaseAlign = GV->getAlignment();
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000445 if (DL && BaseAlign == 0 && GTy->isSized())
446 BaseAlign = DL->getABITypeAlignment(GTy);
Duncan Sands3f4d0b12012-09-25 10:00:49 +0000447 }
Dan Gohman98bc4372010-04-08 18:47:09 +0000448 }
Dan Gohman20a2ae92013-01-31 02:00:45 +0000449
450 // Accesses from before the start or after the end of the object are not
451 // defined.
452 Assert1(Size == AliasAnalysis::UnknownSize ||
453 BaseSize == AliasAnalysis::UnknownSize ||
454 (Offset >= 0 && Offset + Size <= BaseSize),
455 "Undefined behavior: Buffer overflow", &I);
456
457 // Accesses that say that the memory is more aligned than it is are not
458 // defined.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000459 if (DL && Align == 0 && Ty && Ty->isSized())
460 Align = DL->getABITypeAlignment(Ty);
Dan Gohman20a2ae92013-01-31 02:00:45 +0000461 Assert1(!BaseAlign || Align <= MinAlign(BaseAlign, Offset),
462 "Undefined behavior: Memory reference address is misaligned", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000463 }
464}
465
466void Lint::visitLoadInst(LoadInst &I) {
Dan Gohman0fa67e42010-05-28 21:43:57 +0000467 visitMemoryReference(I, I.getPointerOperand(),
468 AA->getTypeStoreSize(I.getType()), I.getAlignment(),
469 I.getType(), MemRef::Read);
Dan Gohman98bc4372010-04-08 18:47:09 +0000470}
471
472void Lint::visitStoreInst(StoreInst &I) {
Dan Gohman0fa67e42010-05-28 21:43:57 +0000473 visitMemoryReference(I, I.getPointerOperand(),
474 AA->getTypeStoreSize(I.getOperand(0)->getType()),
475 I.getAlignment(),
476 I.getOperand(0)->getType(), MemRef::Write);
Dan Gohman98bc4372010-04-08 18:47:09 +0000477}
478
Dan Gohman9ba08a42010-04-09 01:39:53 +0000479void Lint::visitXor(BinaryOperator &I) {
480 Assert1(!isa<UndefValue>(I.getOperand(0)) ||
481 !isa<UndefValue>(I.getOperand(1)),
482 "Undefined result: xor(undef, undef)", &I);
483}
484
485void Lint::visitSub(BinaryOperator &I) {
486 Assert1(!isa<UndefValue>(I.getOperand(0)) ||
487 !isa<UndefValue>(I.getOperand(1)),
488 "Undefined result: sub(undef, undef)", &I);
489}
490
Dan Gohman7808d492010-04-08 23:05:57 +0000491void Lint::visitLShr(BinaryOperator &I) {
492 if (ConstantInt *CI =
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000493 dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
Dan Gohman7808d492010-04-08 23:05:57 +0000494 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000495 "Undefined result: Shift count out of range", &I);
Dan Gohman7808d492010-04-08 23:05:57 +0000496}
497
498void Lint::visitAShr(BinaryOperator &I) {
499 if (ConstantInt *CI =
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000500 dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
Dan Gohman7808d492010-04-08 23:05:57 +0000501 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000502 "Undefined result: Shift count out of range", &I);
Dan Gohman7808d492010-04-08 23:05:57 +0000503}
504
505void Lint::visitShl(BinaryOperator &I) {
506 if (ConstantInt *CI =
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000507 dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
Dan Gohman7808d492010-04-08 23:05:57 +0000508 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000509 "Undefined result: Shift count out of range", &I);
Dan Gohman7808d492010-04-08 23:05:57 +0000510}
511
Hal Finkel60db0582014-09-07 18:57:58 +0000512static bool isZero(Value *V, const DataLayout *DL, DominatorTree *DT,
513 AssumptionTracker *AT) {
Dan Gohman9ba08a42010-04-09 01:39:53 +0000514 // Assume undef could be zero.
Matt Arsenault5faa6692013-08-26 23:29:33 +0000515 if (isa<UndefValue>(V))
516 return true;
Dan Gohman9ba08a42010-04-09 01:39:53 +0000517
Matt Arsenault5faa6692013-08-26 23:29:33 +0000518 VectorType *VecTy = dyn_cast<VectorType>(V->getType());
519 if (!VecTy) {
520 unsigned BitWidth = V->getType()->getIntegerBitWidth();
521 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
Hal Finkel60db0582014-09-07 18:57:58 +0000522 computeKnownBits(V, KnownZero, KnownOne, DL,
523 0, AT, dyn_cast<Instruction>(V), DT);
Matt Arsenault5faa6692013-08-26 23:29:33 +0000524 return KnownZero.isAllOnesValue();
525 }
526
527 // Per-component check doesn't work with zeroinitializer
528 Constant *C = dyn_cast<Constant>(V);
529 if (!C)
530 return false;
531
532 if (C->isZeroValue())
533 return true;
534
535 // For a vector, KnownZero will only be true if all values are zero, so check
536 // this per component
537 unsigned BitWidth = VecTy->getElementType()->getIntegerBitWidth();
538 for (unsigned I = 0, N = VecTy->getNumElements(); I != N; ++I) {
539 Constant *Elem = C->getAggregateElement(I);
540 if (isa<UndefValue>(Elem))
541 return true;
542
543 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
Jay Foada0653a32014-05-14 21:14:37 +0000544 computeKnownBits(Elem, KnownZero, KnownOne, DL);
Matt Arsenault5faa6692013-08-26 23:29:33 +0000545 if (KnownZero.isAllOnesValue())
546 return true;
547 }
548
549 return false;
Dan Gohman98bc4372010-04-08 18:47:09 +0000550}
551
552void Lint::visitSDiv(BinaryOperator &I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000553 Assert1(!isZero(I.getOperand(1), DL, DT, AT),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000554 "Undefined behavior: Division by zero", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000555}
556
557void Lint::visitUDiv(BinaryOperator &I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000558 Assert1(!isZero(I.getOperand(1), DL, DT, AT),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000559 "Undefined behavior: Division by zero", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000560}
561
562void Lint::visitSRem(BinaryOperator &I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000563 Assert1(!isZero(I.getOperand(1), DL, DT, AT),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000564 "Undefined behavior: Division by zero", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000565}
566
567void Lint::visitURem(BinaryOperator &I) {
Hal Finkel60db0582014-09-07 18:57:58 +0000568 Assert1(!isZero(I.getOperand(1), DL, DT, AT),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000569 "Undefined behavior: Division by zero", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000570}
571
572void Lint::visitAllocaInst(AllocaInst &I) {
573 if (isa<ConstantInt>(I.getArraySize()))
574 // This isn't undefined behavior, it's just an obvious pessimization.
575 Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000576 "Pessimization: Static alloca outside of entry block", &I);
Dan Gohman1e33b182010-07-06 15:23:00 +0000577
578 // TODO: Check for an unusual size (MSB set?)
Dan Gohman98bc4372010-04-08 18:47:09 +0000579}
580
581void Lint::visitVAArgInst(VAArgInst &I) {
Craig Topper9f008862014-04-15 04:59:12 +0000582 visitMemoryReference(I, I.getOperand(0), AliasAnalysis::UnknownSize, 0,
583 nullptr, MemRef::Read | MemRef::Write);
Dan Gohman98bc4372010-04-08 18:47:09 +0000584}
585
586void Lint::visitIndirectBrInst(IndirectBrInst &I) {
Craig Topper9f008862014-04-15 04:59:12 +0000587 visitMemoryReference(I, I.getAddress(), AliasAnalysis::UnknownSize, 0,
588 nullptr, MemRef::Branchee);
Dan Gohmand8968da2010-08-02 23:06:43 +0000589
590 Assert1(I.getNumDestinations() != 0,
591 "Undefined behavior: indirectbr with no destinations", &I);
Dan Gohman98bc4372010-04-08 18:47:09 +0000592}
593
Dan Gohman7808d492010-04-08 23:05:57 +0000594void Lint::visitExtractElementInst(ExtractElementInst &I) {
595 if (ConstantInt *CI =
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000596 dyn_cast<ConstantInt>(findValue(I.getIndexOperand(),
597 /*OffsetOk=*/false)))
Dan Gohman7808d492010-04-08 23:05:57 +0000598 Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000599 "Undefined result: extractelement index out of range", &I);
Dan Gohman7808d492010-04-08 23:05:57 +0000600}
601
602void Lint::visitInsertElementInst(InsertElementInst &I) {
603 if (ConstantInt *CI =
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000604 dyn_cast<ConstantInt>(findValue(I.getOperand(2),
605 /*OffsetOk=*/false)))
Dan Gohman7808d492010-04-08 23:05:57 +0000606 Assert1(CI->getValue().ult(I.getType()->getNumElements()),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000607 "Undefined result: insertelement index out of range", &I);
608}
609
610void Lint::visitUnreachableInst(UnreachableInst &I) {
611 // This isn't undefined behavior, it's merely suspicious.
612 Assert1(&I == I.getParent()->begin() ||
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000613 std::prev(BasicBlock::iterator(&I))->mayHaveSideEffects(),
Dan Gohman9ba08a42010-04-09 01:39:53 +0000614 "Unusual: unreachable immediately preceded by instruction without "
615 "side effects", &I);
Dan Gohman7808d492010-04-08 23:05:57 +0000616}
617
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000618/// findValue - Look through bitcasts and simple memory reference patterns
619/// to identify an equivalent, but more informative, value. If OffsetOk
620/// is true, look through getelementptrs with non-zero offsets too.
621///
622/// Most analysis passes don't require this logic, because instcombine
623/// will simplify most of these kinds of things away. But it's a goal of
624/// this Lint pass to be useful even on non-optimized IR.
625Value *Lint::findValue(Value *V, bool OffsetOk) const {
Dan Gohman862f0342010-05-28 16:45:33 +0000626 SmallPtrSet<Value *, 4> Visited;
627 return findValueImpl(V, OffsetOk, Visited);
628}
629
630/// findValueImpl - Implementation helper for findValue.
631Value *Lint::findValueImpl(Value *V, bool OffsetOk,
Craig Topper71b7b682014-08-21 05:55:13 +0000632 SmallPtrSetImpl<Value *> &Visited) const {
Dan Gohman862f0342010-05-28 16:45:33 +0000633 // Detect self-referential values.
634 if (!Visited.insert(V))
635 return UndefValue::get(V->getType());
636
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000637 // TODO: Look through sext or zext cast, when the result is known to
638 // be interpreted as signed or unsigned, respectively.
Dan Gohman0fa67e42010-05-28 21:43:57 +0000639 // TODO: Look through eliminable cast pairs.
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000640 // TODO: Look through calls with unique return values.
641 // TODO: Look through vector insert/extract/shuffle.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000642 V = OffsetOk ? GetUnderlyingObject(V, DL) : V->stripPointerCasts();
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000643 if (LoadInst *L = dyn_cast<LoadInst>(V)) {
644 BasicBlock::iterator BBI = L;
645 BasicBlock *BB = L->getParent();
Dan Gohmanc575ec62010-05-28 17:44:00 +0000646 SmallPtrSet<BasicBlock *, 4> VisitedBlocks;
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000647 for (;;) {
Dan Gohmanc575ec62010-05-28 17:44:00 +0000648 if (!VisitedBlocks.insert(BB)) break;
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000649 if (Value *U = FindAvailableLoadedValue(L->getPointerOperand(),
650 BB, BBI, 6, AA))
Dan Gohman862f0342010-05-28 16:45:33 +0000651 return findValueImpl(U, OffsetOk, Visited);
Dan Gohmanc575ec62010-05-28 17:44:00 +0000652 if (BBI != BB->begin()) break;
653 BB = BB->getUniquePredecessor();
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000654 if (!BB) break;
655 BBI = BB->end();
656 }
Dan Gohman0fa67e42010-05-28 21:43:57 +0000657 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
Duncan Sands7412f6e2010-11-17 04:30:22 +0000658 if (Value *W = PN->hasConstantValue())
Duncan Sandsec7a6ec2010-11-17 10:23:23 +0000659 if (W != V)
660 return findValueImpl(W, OffsetOk, Visited);
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000661 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
Matt Arsenaulta236ea52014-03-06 17:33:55 +0000662 if (CI->isNoopCast(DL))
Dan Gohman862f0342010-05-28 16:45:33 +0000663 return findValueImpl(CI->getOperand(0), OffsetOk, Visited);
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000664 } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) {
665 if (Value *W = FindInsertedValue(Ex->getAggregateOperand(),
Jay Foad57aa6362011-07-13 10:26:04 +0000666 Ex->getIndices()))
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000667 if (W != V)
Dan Gohman862f0342010-05-28 16:45:33 +0000668 return findValueImpl(W, OffsetOk, Visited);
Dan Gohman0fa67e42010-05-28 21:43:57 +0000669 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
670 // Same as above, but for ConstantExpr instead of Instruction.
671 if (Instruction::isCast(CE->getOpcode())) {
672 if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()),
673 CE->getOperand(0)->getType(),
674 CE->getType(),
Matt Arsenaulta236ea52014-03-06 17:33:55 +0000675 DL ? DL->getIntPtrType(V->getType()) :
Dan Gohman0fa67e42010-05-28 21:43:57 +0000676 Type::getInt64Ty(V->getContext())))
677 return findValueImpl(CE->getOperand(0), OffsetOk, Visited);
678 } else if (CE->getOpcode() == Instruction::ExtractValue) {
Jay Foad0091fe82011-04-13 15:22:40 +0000679 ArrayRef<unsigned> Indices = CE->getIndices();
Jay Foad57aa6362011-07-13 10:26:04 +0000680 if (Value *W = FindInsertedValue(CE->getOperand(0), Indices))
Dan Gohman0fa67e42010-05-28 21:43:57 +0000681 if (W != V)
682 return findValueImpl(W, OffsetOk, Visited);
683 }
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000684 }
685
686 // As a last resort, try SimplifyInstruction or constant folding.
687 if (Instruction *Inst = dyn_cast<Instruction>(V)) {
Hal Finkel60db0582014-09-07 18:57:58 +0000688 if (Value *W = SimplifyInstruction(Inst, DL, TLI, DT, AT))
Duncan Sands64e41cf2010-11-17 08:35:29 +0000689 return findValueImpl(W, OffsetOk, Visited);
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000690 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000691 if (Value *W = ConstantFoldConstantExpression(CE, DL, TLI))
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000692 if (W != V)
Dan Gohman862f0342010-05-28 16:45:33 +0000693 return findValueImpl(W, OffsetOk, Visited);
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000694 }
695
696 return V;
697}
698
Dan Gohman98bc4372010-04-08 18:47:09 +0000699//===----------------------------------------------------------------------===//
700// Implement the public interfaces to this file...
701//===----------------------------------------------------------------------===//
702
703FunctionPass *llvm::createLintPass() {
704 return new Lint();
705}
706
707/// lintFunction - Check a function for errors, printing messages on stderr.
708///
709void llvm::lintFunction(const Function &f) {
710 Function &F = const_cast<Function&>(f);
711 assert(!F.isDeclaration() && "Cannot lint external functions");
712
713 FunctionPassManager FPM(F.getParent());
714 Lint *V = new Lint();
715 FPM.add(V);
716 FPM.run(F);
717}
718
719/// lintModule - Check a module for errors, printing messages on stderr.
Dan Gohman98bc4372010-04-08 18:47:09 +0000720///
Dan Gohman084bcb12010-05-26 22:28:53 +0000721void llvm::lintModule(const Module &M) {
Dan Gohman98bc4372010-04-08 18:47:09 +0000722 PassManager PM;
723 Lint *V = new Lint();
724 PM.add(V);
725 PM.run(const_cast<Module&>(M));
Dan Gohman98bc4372010-04-08 18:47:09 +0000726}