blob: b8e324ee29e06e287908ea2ba17f449fff779eda [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
Pete Cooper1d078692015-12-14 20:29:16 +000090void PHINode::anchor() {}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000093 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
94 ReservedSpace(PN.getNumOperands()) {
95 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000096 std::copy(PN.op_begin(), PN.op_end(), op_begin());
97 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// removeIncomingValue - Remove an incoming value. This is useful if a
102// predecessor basic block is deleted.
103Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105
106 // Move everything after this operand down.
107 //
108 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
112 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113
114 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000115 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000121 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 eraseFromParent();
123 }
124 return Removed;
125}
126
Jay Foade98f29d2011-04-01 08:00:58 +0000127/// growOperands - grow operands - This grows the operand list in response
128/// to a push_back style of operation. This grows the number of ops by 1.5
129/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130///
Jay Foade98f29d2011-04-01 08:00:58 +0000131void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000132 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000133 unsigned NumOps = e + e / 2;
134 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
135
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000137 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Nate Begemanb3923212005-08-04 23:24:19 +0000140/// hasConstantValue - If the specified PHI node always merges together the same
141/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000142Value *PHINode::hasConstantValue() const {
143 // Exploit the fact that phi nodes always have at least one entry.
144 Value *ConstantValue = getIncomingValue(0);
145 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000146 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
147 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000148 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000149 // The case where the first value is this PHI.
150 ConstantValue = getIncomingValue(i);
151 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000152 if (ConstantValue == this)
153 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000155}
156
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000157/// hasConstantOrUndefValue - Whether the specified PHI node always merges
158/// together the same value, assuming that undefs result in the same value as
159/// non-undefs.
160/// Unlike \ref hasConstantValue, this does not return a value because the
161/// unique non-undef incoming value need not dominate the PHI node.
162bool PHINode::hasConstantOrUndefValue() const {
163 Value *ConstantValue = nullptr;
164 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
165 Value *Incoming = getIncomingValue(i);
166 if (Incoming != this && !isa<UndefValue>(Incoming)) {
167 if (ConstantValue && ConstantValue != Incoming)
168 return false;
169 ConstantValue = Incoming;
170 }
171 }
172 return true;
173}
174
Bill Wendlingfae14752011-08-12 20:24:12 +0000175//===----------------------------------------------------------------------===//
176// LandingPadInst Implementation
177//===----------------------------------------------------------------------===//
178
David Majnemer7fddecc2015-06-17 20:52:32 +0000179LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
180 const Twine &NameStr, Instruction *InsertBefore)
181 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
182 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000183}
184
David Majnemer7fddecc2015-06-17 20:52:32 +0000185LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
186 const Twine &NameStr, BasicBlock *InsertAtEnd)
187 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
188 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000189}
190
191LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000192 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
193 LP.getNumOperands()),
194 ReservedSpace(LP.getNumOperands()) {
195 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000196 Use *OL = getOperandList();
197 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000198 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
199 OL[I] = InOL[I];
200
201 setCleanup(LP.isCleanup());
202}
203
David Majnemer7fddecc2015-06-17 20:52:32 +0000204LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000205 const Twine &NameStr,
206 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000207 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000208}
209
David Majnemer7fddecc2015-06-17 20:52:32 +0000210LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000211 const Twine &NameStr,
212 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000213 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000214}
215
David Majnemer7fddecc2015-06-17 20:52:32 +0000216void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000217 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000218 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000219 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000220 setName(NameStr);
221 setCleanup(false);
222}
223
224/// growOperands - grow operands - This grows the operand list in response to a
225/// push_back style of operation. This grows the number of ops by 2 times.
226void LandingPadInst::growOperands(unsigned Size) {
227 unsigned e = getNumOperands();
228 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000229 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000230 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000231}
232
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000233void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000234 unsigned OpNo = getNumOperands();
235 growOperands(1);
236 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000237 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000238 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000239}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240
241//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242// CallInst Implementation
243//===----------------------------------------------------------------------===//
244
Gordon Henriksen14a55692007-12-10 02:14:30 +0000245CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246}
247
David Blaikie348de692015-04-23 21:36:23 +0000248void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000249 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000250 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000251 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
252 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000253 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254
Jay Foad5bd375a2011-07-15 08:37:34 +0000255#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000256 assert((Args.size() == FTy->getNumParams() ||
257 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000258 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000259
260 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000261 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000262 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000263 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264#endif
265
266 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000267
268 auto It = populateBundleOperandInfos(Bundles, Args.size());
269 (void)It;
270 assert(It + 1 == op_end() && "Should add up!");
271
Jay Foad5bd375a2011-07-15 08:37:34 +0000272 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273}
274
Jay Foad5bd375a2011-07-15 08:37:34 +0000275void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000276 FTy =
277 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000278 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000279 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000282
283 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
Daniel Dunbar4975db62009-07-25 04:41:11 +0000286CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 Instruction *InsertBefore)
288 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
289 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000290 Instruction::Call,
291 OperandTraits<CallInst>::op_end(this) - 1,
292 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000293 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294}
295
Daniel Dunbar4975db62009-07-25 04:41:11 +0000296CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 BasicBlock *InsertAtEnd)
298 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
299 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000300 Instruction::Call,
301 OperandTraits<CallInst>::op_end(this) - 1,
302 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000303 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304}
305
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000307 : Instruction(CI.getType(), Instruction::Call,
308 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
309 CI.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000310 Attrs(CI.Attrs), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000311 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000312 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000313
Jay Foad5bd375a2011-07-15 08:37:34 +0000314 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000315 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
316 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000317 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Sanjoy Das2d161452015-11-18 06:23:38 +0000320CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
321 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000322 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000323
324 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
325 InsertPt);
326 NewCI->setTailCallKind(CI->getTailCallKind());
327 NewCI->setCallingConv(CI->getCallingConv());
328 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000329 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000330 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000331 return NewCI;
332}
333
Hal Finkele87ad542016-07-10 23:01:32 +0000334Value *CallInst::getReturnedArgOperand() const {
335 unsigned Index;
336
Reid Klecknerb5180542017-03-21 16:57:19 +0000337 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000338 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000339 if (const Function *F = getCalledFunction())
340 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
341 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000342 return getArgOperand(Index - AttributeList::FirstArgIndex);
343
Hal Finkele87ad542016-07-10 23:01:32 +0000344 return nullptr;
345}
346
Amaury Sechet392638d2016-06-14 20:27:35 +0000347void CallInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000348 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000349 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000350 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000351}
352
Amaury Secheta65a2372016-06-15 05:14:29 +0000353void CallInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000354 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000355 PAL = PAL.addAttribute(getContext(), i, Attr);
356 setAttributes(PAL);
357}
358
Reid Klecknera0b45f42017-05-03 18:17:31 +0000359void CallInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
360 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
361}
362
Amaury Sechet392638d2016-06-14 20:27:35 +0000363void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000364 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000365 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000366 setAttributes(PAL);
367}
368
Amaury Sechet6100adf2016-06-15 17:50:39 +0000369void CallInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000370 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000371 PAL = PAL.removeAttribute(getContext(), i, Kind);
372 setAttributes(PAL);
373}
374
Reid Klecknera0b45f42017-05-03 18:17:31 +0000375void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
376 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
377}
378
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000379void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000380 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000381 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
382 setAttributes(PAL);
383}
384
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000385void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000386 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000387 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
388 setAttributes(PAL);
389}
390
Reid Klecknerfb502d22017-04-14 20:19:02 +0000391bool CallInst::hasRetAttr(Attribute::AttrKind Kind) const {
392 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
393 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000394
Reid Klecknerfb502d22017-04-14 20:19:02 +0000395 // Look at the callee, if available.
396 if (const Function *F = getCalledFunction())
397 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
398 return false;
399}
400
401bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
402 assert(i < getNumArgOperands() && "Param index out of bounds!");
403
404 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000405 return true;
406 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000407 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000408 return false;
409}
410
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000411bool CallInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000412 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000413 // There are getNumOperands() - 1 data operands. The last operand is the
414 // callee.
415 assert(i < getNumOperands() && "Data operand index out of bounds!");
416
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000417 // The attribute A can either be directly specified, if the operand in
418 // question is a call argument; or be indirectly implied by the kind of its
419 // containing operand bundle, if the operand is a bundle operand.
420
Reid Klecknerfb502d22017-04-14 20:19:02 +0000421 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
422 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000423 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000424 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000425
426 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
427 "Must be either a call argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000428 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000429}
430
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000431/// IsConstantOne - Return true only if val is constant int 1
432static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000433 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000434 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
435 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000436}
437
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000438static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000439 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000440 Type *AllocTy, Value *AllocSize,
441 Value *ArraySize,
442 ArrayRef<OperandBundleDef> OpB,
443 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000444 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000445 "createMalloc needs either InsertBefore or InsertAtEnd");
446
447 // malloc(type) becomes:
448 // bitcast (i8* malloc(typeSize)) to type*
449 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000450 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000451 if (!ArraySize)
452 ArraySize = ConstantInt::get(IntPtrTy, 1);
453 else if (ArraySize->getType() != IntPtrTy) {
454 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000455 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
456 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000457 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000458 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
459 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000460 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000462 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000463 if (IsConstantOne(AllocSize)) {
464 AllocSize = ArraySize; // Operand * 1 = Operand
465 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
466 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
467 false /*ZExt*/);
468 // Malloc arg is constant product of type size and array size
469 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
470 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000471 // Multiply type size by the array size...
472 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000473 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
474 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000475 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000476 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
477 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000478 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000479 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000480
Victor Hernandez788eaab2009-09-18 19:20:02 +0000481 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000482 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000483 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
484 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000485 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000486 Value *MallocFunc = MallocF;
487 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000488 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000489 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000490 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000491 CallInst *MCall = nullptr;
492 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000493 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000494 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
495 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000496 Result = MCall;
497 if (Result->getType() != AllocPtrType)
498 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000499 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000500 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000501 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000502 Result = MCall;
503 if (Result->getType() != AllocPtrType) {
504 InsertAtEnd->getInstList().push_back(MCall);
505 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000506 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000507 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000508 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000509 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000510 if (Function *F = dyn_cast<Function>(MallocFunc)) {
511 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000512 if (!F->returnDoesNotAlias())
513 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000514 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000515 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000516
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000517 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000518}
519
520/// CreateMalloc - Generate the IR for a call to malloc:
521/// 1. Compute the malloc call's argument as the specified type's size,
522/// possibly multiplied by the array size if the array size is not
523/// constant 1.
524/// 2. Call malloc with that argument.
525/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000526Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000527 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000528 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000529 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000530 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000531 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000532 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000533}
David Majnemerfadc6db2016-04-29 08:07:22 +0000534Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
535 Type *IntPtrTy, Type *AllocTy,
536 Value *AllocSize, Value *ArraySize,
537 ArrayRef<OperandBundleDef> OpB,
538 Function *MallocF,
539 const Twine &Name) {
540 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
541 ArraySize, OpB, MallocF, Name);
542}
543
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000544
545/// CreateMalloc - Generate the IR for a call to malloc:
546/// 1. Compute the malloc call's argument as the specified type's size,
547/// possibly multiplied by the array size if the array size is not
548/// constant 1.
549/// 2. Call malloc with that argument.
550/// 3. Bitcast the result of the malloc call to the specified type.
551/// Note: This function does not add the bitcast to the basic block, that is the
552/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000553Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000554 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000555 Value *AllocSize, Value *ArraySize,
556 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000557 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000558 ArraySize, None, MallocF, Name);
559}
560Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
561 Type *IntPtrTy, Type *AllocTy,
562 Value *AllocSize, Value *ArraySize,
563 ArrayRef<OperandBundleDef> OpB,
564 Function *MallocF, const Twine &Name) {
565 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
566 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000567}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000568
David Majnemerfadc6db2016-04-29 08:07:22 +0000569static Instruction *createFree(Value *Source,
570 ArrayRef<OperandBundleDef> Bundles,
571 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000572 BasicBlock *InsertAtEnd) {
573 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
574 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000575 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000576 "Can not free something of nonpointer type!");
577
Ana Pazosb3596022016-02-03 21:34:39 +0000578 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
579 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000580
Chris Lattner229907c2011-07-18 04:54:35 +0000581 Type *VoidTy = Type::getVoidTy(M->getContext());
582 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000583 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000584 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000585 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000586 Value *PtrCast = Source;
587 if (InsertBefore) {
588 if (Source->getType() != IntPtrTy)
589 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000590 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000591 } else {
592 if (Source->getType() != IntPtrTy)
593 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000594 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000595 }
596 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000597 if (Function *F = dyn_cast<Function>(FreeFunc))
598 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000599
600 return Result;
601}
602
603/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000604Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000605 return createFree(Source, None, InsertBefore, nullptr);
606}
607Instruction *CallInst::CreateFree(Value *Source,
608 ArrayRef<OperandBundleDef> Bundles,
609 Instruction *InsertBefore) {
610 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000611}
612
613/// CreateFree - Generate the IR for a call to the builtin free function.
614/// Note: This function does not add the call to the basic block, that is the
615/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000616Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000617 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
618 assert(FreeCall && "CreateFree did not create a CallInst");
619 return FreeCall;
620}
621Instruction *CallInst::CreateFree(Value *Source,
622 ArrayRef<OperandBundleDef> Bundles,
623 BasicBlock *InsertAtEnd) {
624 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000625 assert(FreeCall && "CreateFree did not create a CallInst");
626 return FreeCall;
627}
628
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629//===----------------------------------------------------------------------===//
630// InvokeInst Implementation
631//===----------------------------------------------------------------------===//
632
David Blaikie3e807092015-05-13 18:35:26 +0000633void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
634 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000635 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000636 const Twine &NameStr) {
637 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000638
Sanjoy Das9303c242015-09-24 19:14:18 +0000639 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
640 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000641 Op<-3>() = Fn;
642 Op<-2>() = IfNormal;
643 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000644
645#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000646 assert(((Args.size() == FTy->getNumParams()) ||
647 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000648 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000649
Jay Foad5bd375a2011-07-15 08:37:34 +0000650 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000651 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000652 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000653 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000654#endif
655
656 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000657
658 auto It = populateBundleOperandInfos(Bundles, Args.size());
659 (void)It;
660 assert(It + 3 == op_end() && "Should add up!");
661
Jay Foad5bd375a2011-07-15 08:37:34 +0000662 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663}
664
Misha Brukmanb1c93172005-04-21 23:48:37 +0000665InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000666 : TerminatorInst(II.getType(), Instruction::Invoke,
667 OperandTraits<InvokeInst>::op_end(this) -
668 II.getNumOperands(),
669 II.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000670 Attrs(II.Attrs), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000671 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000672 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000673 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
674 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000675 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000676}
677
Sanjoy Das2d161452015-11-18 06:23:38 +0000678InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
679 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000680 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000681
682 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
683 II->getUnwindDest(), Args, OpB,
684 II->getName(), InsertPt);
685 NewII->setCallingConv(II->getCallingConv());
686 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000687 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000688 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000689 return NewII;
690}
691
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000692BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
693 return getSuccessor(idx);
694}
695unsigned InvokeInst::getNumSuccessorsV() const {
696 return getNumSuccessors();
697}
698void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
699 return setSuccessor(idx, B);
700}
701
Hal Finkele87ad542016-07-10 23:01:32 +0000702Value *InvokeInst::getReturnedArgOperand() const {
703 unsigned Index;
704
Reid Klecknerb5180542017-03-21 16:57:19 +0000705 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000706 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000707 if (const Function *F = getCalledFunction())
708 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
709 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000710 return getArgOperand(Index - AttributeList::FirstArgIndex);
711
Hal Finkele87ad542016-07-10 23:01:32 +0000712 return nullptr;
713}
714
Reid Klecknerfb502d22017-04-14 20:19:02 +0000715bool InvokeInst::hasRetAttr(Attribute::AttrKind Kind) const {
716 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
717 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000718
Reid Klecknerfb502d22017-04-14 20:19:02 +0000719 // Look at the callee, if available.
720 if (const Function *F = getCalledFunction())
721 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
722 return false;
723}
724
725bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
726 assert(i < getNumArgOperands() && "Param index out of bounds!");
727
728 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000729 return true;
730 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000731 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000732 return false;
733}
734
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000735bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000736 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000737 // There are getNumOperands() - 3 data operands. The last three operands are
738 // the callee and the two successor basic blocks.
739 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
740
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000741 // The attribute A can either be directly specified, if the operand in
742 // question is an invoke argument; or be indirectly implied by the kind of its
743 // containing operand bundle, if the operand is a bundle operand.
744
Reid Klecknerfb502d22017-04-14 20:19:02 +0000745 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
746 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000747 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000748 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000749
750 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
751 "Must be either an invoke argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000752 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000753}
754
Amaury Sechet392638d2016-06-14 20:27:35 +0000755void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000756 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000757 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000758 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000759}
760
Amaury Secheta65a2372016-06-15 05:14:29 +0000761void InvokeInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000762 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000763 PAL = PAL.addAttribute(getContext(), i, Attr);
764 setAttributes(PAL);
765}
766
Reid Klecknera0b45f42017-05-03 18:17:31 +0000767void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
768 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
769}
770
Amaury Sechet392638d2016-06-14 20:27:35 +0000771void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000772 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000773 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000774 setAttributes(PAL);
775}
776
Amaury Sechet6100adf2016-06-15 17:50:39 +0000777void InvokeInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000778 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000779 PAL = PAL.removeAttribute(getContext(), i, Kind);
780 setAttributes(PAL);
781}
782
Reid Klecknera0b45f42017-05-03 18:17:31 +0000783void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
784 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
785}
786
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000787void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000788 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000789 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
790 setAttributes(PAL);
791}
792
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000793void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000794 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000795 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
796 setAttributes(PAL);
797}
798
Bill Wendlingfae14752011-08-12 20:24:12 +0000799LandingPadInst *InvokeInst::getLandingPadInst() const {
800 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
801}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000802
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803//===----------------------------------------------------------------------===//
804// ReturnInst Implementation
805//===----------------------------------------------------------------------===//
806
Chris Lattner2195fc42007-02-24 00:55:48 +0000807ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000808 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000809 OperandTraits<ReturnInst>::op_end(this) -
810 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000811 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000812 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000813 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000814 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000815}
816
Owen Anderson55f1c092009-08-13 21:58:54 +0000817ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
818 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000819 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
820 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000821 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000822 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000823}
Owen Anderson55f1c092009-08-13 21:58:54 +0000824ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
825 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000826 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
827 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000828 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000829 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000830}
Owen Anderson55f1c092009-08-13 21:58:54 +0000831ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
832 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000833 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000834}
835
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000836unsigned ReturnInst::getNumSuccessorsV() const {
837 return getNumSuccessors();
838}
839
Devang Patelae682fb2008-02-26 17:56:20 +0000840/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
841/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000842void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000843 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000844}
845
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000846BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000847 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000848}
849
Devang Patel59643e52008-02-23 00:35:18 +0000850ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000851}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000852
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000853//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000854// ResumeInst Implementation
855//===----------------------------------------------------------------------===//
856
857ResumeInst::ResumeInst(const ResumeInst &RI)
858 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
859 OperandTraits<ResumeInst>::op_begin(this), 1) {
860 Op<0>() = RI.Op<0>();
861}
862
863ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
864 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
865 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
866 Op<0>() = Exn;
867}
868
869ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
870 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
871 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
872 Op<0>() = Exn;
873}
874
875unsigned ResumeInst::getNumSuccessorsV() const {
876 return getNumSuccessors();
877}
878
879void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
880 llvm_unreachable("ResumeInst has no successors!");
881}
882
883BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
884 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000885}
886
887//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000888// CleanupReturnInst Implementation
889//===----------------------------------------------------------------------===//
890
891CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
892 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
893 OperandTraits<CleanupReturnInst>::op_end(this) -
894 CRI.getNumOperands(),
895 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000896 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000897 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000898 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000899 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000900}
901
David Majnemer8a1c45d2015-12-12 05:38:55 +0000902void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000903 if (UnwindBB)
904 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000905
David Majnemer8a1c45d2015-12-12 05:38:55 +0000906 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000907 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000908 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000909}
910
David Majnemer8a1c45d2015-12-12 05:38:55 +0000911CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
912 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000913 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
914 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000915 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
916 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000917 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000918}
919
David Majnemer8a1c45d2015-12-12 05:38:55 +0000920CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
921 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000922 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
923 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000924 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
925 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000926 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000927}
928
929BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
930 assert(Idx == 0);
931 return getUnwindDest();
932}
933unsigned CleanupReturnInst::getNumSuccessorsV() const {
934 return getNumSuccessors();
935}
936void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
937 assert(Idx == 0);
938 setUnwindDest(B);
939}
940
941//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000942// CatchReturnInst Implementation
943//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000944void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000945 Op<0>() = CatchPad;
946 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000947}
David Majnemer654e1302015-07-31 17:58:14 +0000948
949CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
950 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000951 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
952 Op<0>() = CRI.Op<0>();
953 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000954}
955
David Majnemer8a1c45d2015-12-12 05:38:55 +0000956CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000957 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000958 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000959 OperandTraits<CatchReturnInst>::op_begin(this), 2,
960 InsertBefore) {
961 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000962}
963
David Majnemer8a1c45d2015-12-12 05:38:55 +0000964CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000965 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000966 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000967 OperandTraits<CatchReturnInst>::op_begin(this), 2,
968 InsertAtEnd) {
969 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000970}
971
972BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000973 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000974 return getSuccessor();
975}
976unsigned CatchReturnInst::getNumSuccessorsV() const {
977 return getNumSuccessors();
978}
979void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000980 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000981 setSuccessor(B);
982}
983
984//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000985// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000986//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000987
988CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
989 unsigned NumReservedValues,
990 const Twine &NameStr,
991 Instruction *InsertBefore)
992 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
993 InsertBefore) {
994 if (UnwindDest)
995 ++NumReservedValues;
996 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000997 setName(NameStr);
998}
999
David Majnemer8a1c45d2015-12-12 05:38:55 +00001000CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1001 unsigned NumReservedValues,
1002 const Twine &NameStr, BasicBlock *InsertAtEnd)
1003 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +00001004 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00001005 if (UnwindDest)
1006 ++NumReservedValues;
1007 init(ParentPad, UnwindDest, NumReservedValues + 1);
1008 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001009}
1010
David Majnemer8a1c45d2015-12-12 05:38:55 +00001011CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
1012 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
1013 CSI.getNumOperands()) {
1014 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
1015 setNumHungOffUseOperands(ReservedSpace);
1016 Use *OL = getOperandList();
1017 const Use *InOL = CSI.getOperandList();
1018 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
1019 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +00001020}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001021
1022void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
1023 unsigned NumReservedValues) {
1024 assert(ParentPad && NumReservedValues);
1025
1026 ReservedSpace = NumReservedValues;
1027 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
1028 allocHungoffUses(ReservedSpace);
1029
1030 Op<0>() = ParentPad;
1031 if (UnwindDest) {
1032 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1033 setUnwindDest(UnwindDest);
1034 }
1035}
1036
1037/// growOperands - grow operands - This grows the operand list in response to a
1038/// push_back style of operation. This grows the number of ops by 2 times.
1039void CatchSwitchInst::growOperands(unsigned Size) {
1040 unsigned NumOperands = getNumOperands();
1041 assert(NumOperands >= 1);
1042 if (ReservedSpace >= NumOperands + Size)
1043 return;
1044 ReservedSpace = (NumOperands + Size / 2) * 2;
1045 growHungoffUses(ReservedSpace);
1046}
1047
1048void CatchSwitchInst::addHandler(BasicBlock *Handler) {
1049 unsigned OpNo = getNumOperands();
1050 growOperands(1);
1051 assert(OpNo < ReservedSpace && "Growing didn't work!");
1052 setNumHungOffUseOperands(getNumOperands() + 1);
1053 getOperandList()[OpNo] = Handler;
1054}
1055
Joseph Tremoulet0d808882016-01-05 02:37:41 +00001056void CatchSwitchInst::removeHandler(handler_iterator HI) {
1057 // Move all subsequent handlers up one.
1058 Use *EndDst = op_end() - 1;
1059 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1060 *CurDst = *(CurDst + 1);
1061 // Null out the last handler use.
1062 *EndDst = nullptr;
1063
1064 setNumHungOffUseOperands(getNumOperands() - 1);
1065}
1066
David Majnemer8a1c45d2015-12-12 05:38:55 +00001067BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
1068 return getSuccessor(idx);
1069}
1070unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +00001071 return getNumSuccessors();
1072}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001073void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1074 setSuccessor(idx, B);
1075}
1076
1077//===----------------------------------------------------------------------===//
1078// FuncletPadInst Implementation
1079//===----------------------------------------------------------------------===//
1080void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1081 const Twine &NameStr) {
1082 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1083 std::copy(Args.begin(), Args.end(), op_begin());
1084 setParentPad(ParentPad);
1085 setName(NameStr);
1086}
1087
1088FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1089 : Instruction(FPI.getType(), FPI.getOpcode(),
1090 OperandTraits<FuncletPadInst>::op_end(this) -
1091 FPI.getNumOperands(),
1092 FPI.getNumOperands()) {
1093 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1094 setParentPad(FPI.getParentPad());
1095}
1096
1097FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1098 ArrayRef<Value *> Args, unsigned Values,
1099 const Twine &NameStr, Instruction *InsertBefore)
1100 : Instruction(ParentPad->getType(), Op,
1101 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1102 InsertBefore) {
1103 init(ParentPad, Args, NameStr);
1104}
1105
1106FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1107 ArrayRef<Value *> Args, unsigned Values,
1108 const Twine &NameStr, BasicBlock *InsertAtEnd)
1109 : Instruction(ParentPad->getType(), Op,
1110 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1111 InsertAtEnd) {
1112 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001113}
1114
1115//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001116// UnreachableInst Implementation
1117//===----------------------------------------------------------------------===//
1118
Owen Anderson55f1c092009-08-13 21:58:54 +00001119UnreachableInst::UnreachableInst(LLVMContext &Context,
1120 Instruction *InsertBefore)
1121 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001122 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001123}
Owen Anderson55f1c092009-08-13 21:58:54 +00001124UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1125 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001126 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001127}
1128
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001129unsigned UnreachableInst::getNumSuccessorsV() const {
1130 return getNumSuccessors();
1131}
1132
1133void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001134 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001135}
1136
1137BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001138 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001139}
1140
1141//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001142// BranchInst Implementation
1143//===----------------------------------------------------------------------===//
1144
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001145void BranchInst::AssertOK() {
1146 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001147 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001148 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001149}
1150
Chris Lattner2195fc42007-02-24 00:55:48 +00001151BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001152 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001153 OperandTraits<BranchInst>::op_end(this) - 1,
1154 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001155 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001156 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001157}
1158BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1159 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001160 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001161 OperandTraits<BranchInst>::op_end(this) - 3,
1162 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001163 Op<-1>() = IfTrue;
1164 Op<-2>() = IfFalse;
1165 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001166#ifndef NDEBUG
1167 AssertOK();
1168#endif
1169}
1170
1171BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001172 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001173 OperandTraits<BranchInst>::op_end(this) - 1,
1174 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001175 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001176 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001177}
1178
1179BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1180 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001181 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001182 OperandTraits<BranchInst>::op_end(this) - 3,
1183 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001184 Op<-1>() = IfTrue;
1185 Op<-2>() = IfFalse;
1186 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001187#ifndef NDEBUG
1188 AssertOK();
1189#endif
1190}
1191
1192
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001193BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001194 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001195 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1196 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001197 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001198 if (BI.getNumOperands() != 1) {
1199 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001200 Op<-3>() = BI.Op<-3>();
1201 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001202 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001203 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001204}
1205
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001206void BranchInst::swapSuccessors() {
1207 assert(isConditional() &&
1208 "Cannot swap successors of an unconditional branch");
1209 Op<-1>().swap(Op<-2>());
1210
1211 // Update profile metadata if present and it matches our structural
1212 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001213 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001214}
1215
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001216BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1217 return getSuccessor(idx);
1218}
1219unsigned BranchInst::getNumSuccessorsV() const {
1220 return getNumSuccessors();
1221}
1222void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1223 setSuccessor(idx, B);
1224}
1225
1226
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001227//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001228// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001229//===----------------------------------------------------------------------===//
1230
Owen Andersonb6b25302009-07-14 23:09:55 +00001231static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001232 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001233 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001234 else {
1235 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001236 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001237 assert(Amt->getType()->isIntegerTy() &&
1238 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001239 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001240 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001241}
1242
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001243AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001244 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001245 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001246
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001247AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001248 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001249 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001250
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001251AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001252 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001253 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1254
1255AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1256 const Twine &Name, BasicBlock *InsertAtEnd)
1257 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1258
1259AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1260 unsigned Align, const Twine &Name,
1261 Instruction *InsertBefore)
1262 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1263 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1264 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001265 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001266 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001267 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001268}
1269
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001270AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1271 unsigned Align, const Twine &Name,
1272 BasicBlock *InsertAtEnd)
1273 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1274 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001275 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001276 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001277 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001278 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001279}
1280
Gordon Henriksen14a55692007-12-10 02:14:30 +00001281// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001282AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001283}
1284
Victor Hernandez8acf2952009-10-23 21:09:37 +00001285void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001286 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001287 assert(Align <= MaximumAlignment &&
1288 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001289 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1290 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001291 assert(getAlignment() == Align && "Alignment representation error!");
1292}
1293
Victor Hernandez8acf2952009-10-23 21:09:37 +00001294bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001295 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001296 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001297 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001298}
1299
Chris Lattner8b291e62008-11-26 02:54:17 +00001300/// isStaticAlloca - Return true if this alloca is in the entry block of the
1301/// function and is a constant size. If so, the code generator will fold it
1302/// into the prolog/epilog code, so it is basically free.
1303bool AllocaInst::isStaticAlloca() const {
1304 // Must be constant size.
1305 if (!isa<ConstantInt>(getArraySize())) return false;
1306
1307 // Must be in the entry block.
1308 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001309 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001310}
1311
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001312//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001313// LoadInst Implementation
1314//===----------------------------------------------------------------------===//
1315
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001316void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001317 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001318 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001319 assert(!(isAtomic() && getAlignment() == 0) &&
1320 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001321}
1322
Daniel Dunbar4975db62009-07-25 04:41:11 +00001323LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001324 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001325
Daniel Dunbar4975db62009-07-25 04:41:11 +00001326LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001327 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001328
David Blaikie0c28fd72015-05-20 21:46:30 +00001329LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001330 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001331 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001332
1333LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1334 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001335 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001336
David Blaikieb7a029872015-04-17 19:56:21 +00001337LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001338 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001339 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1340 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001341
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001342LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001343 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001344 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1345 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001346
David Blaikie15d9a4c2015-04-06 20:59:48 +00001347LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001348 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001349 SynchronizationScope SynchScope, Instruction *InsertBef)
1350 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001351 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001352 setVolatile(isVolatile);
1353 setAlignment(Align);
1354 setAtomic(Order, SynchScope);
1355 AssertOK();
1356 setName(Name);
1357}
1358
1359LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1360 unsigned Align, AtomicOrdering Order,
1361 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001362 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001363 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001364 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001365 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001366 setAlignment(Align);
1367 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001368 AssertOK();
1369 setName(Name);
1370}
1371
Daniel Dunbar27096822009-08-11 18:11:15 +00001372LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1373 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1374 Load, Ptr, InsertBef) {
1375 setVolatile(false);
1376 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001377 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001378 AssertOK();
1379 if (Name && Name[0]) setName(Name);
1380}
1381
1382LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1383 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1384 Load, Ptr, InsertAE) {
1385 setVolatile(false);
1386 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001387 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001388 AssertOK();
1389 if (Name && Name[0]) setName(Name);
1390}
1391
David Blaikie0c28fd72015-05-20 21:46:30 +00001392LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001393 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001394 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1395 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001396 setVolatile(isVolatile);
1397 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001398 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001399 AssertOK();
1400 if (Name && Name[0]) setName(Name);
1401}
1402
1403LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1404 BasicBlock *InsertAE)
1405 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1406 Load, Ptr, InsertAE) {
1407 setVolatile(isVolatile);
1408 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001409 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001410 AssertOK();
1411 if (Name && Name[0]) setName(Name);
1412}
1413
Christopher Lamb84485702007-04-22 19:24:39 +00001414void LoadInst::setAlignment(unsigned Align) {
1415 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001416 assert(Align <= MaximumAlignment &&
1417 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001418 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001419 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001420 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001421}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001422
1423//===----------------------------------------------------------------------===//
1424// StoreInst Implementation
1425//===----------------------------------------------------------------------===//
1426
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001427void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001428 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001429 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001430 "Ptr must have pointer type!");
1431 assert(getOperand(0)->getType() ==
1432 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001433 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001434 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001435 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001436}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001437
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001438StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001439 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001440
1441StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001442 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001443
Misha Brukmanb1c93172005-04-21 23:48:37 +00001444StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001445 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001446 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001447
1448StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001449 BasicBlock *InsertAtEnd)
1450 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1451
1452StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1453 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001454 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1455 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001456
1457StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1458 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001459 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1460 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001461
Misha Brukmanb1c93172005-04-21 23:48:37 +00001462StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001463 unsigned Align, AtomicOrdering Order,
1464 SynchronizationScope SynchScope,
1465 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001466 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001467 OperandTraits<StoreInst>::op_begin(this),
1468 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001469 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001470 Op<0>() = val;
1471 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001472 setVolatile(isVolatile);
1473 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001474 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001475 AssertOK();
1476}
1477
1478StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001479 unsigned Align, AtomicOrdering Order,
1480 SynchronizationScope SynchScope,
1481 BasicBlock *InsertAtEnd)
1482 : Instruction(Type::getVoidTy(val->getContext()), Store,
1483 OperandTraits<StoreInst>::op_begin(this),
1484 OperandTraits<StoreInst>::operands(this),
1485 InsertAtEnd) {
1486 Op<0>() = val;
1487 Op<1>() = addr;
1488 setVolatile(isVolatile);
1489 setAlignment(Align);
1490 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001491 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001492}
1493
Christopher Lamb84485702007-04-22 19:24:39 +00001494void StoreInst::setAlignment(unsigned Align) {
1495 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001496 assert(Align <= MaximumAlignment &&
1497 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001498 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001499 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001500 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001501}
1502
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001503//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001504// AtomicCmpXchgInst Implementation
1505//===----------------------------------------------------------------------===//
1506
1507void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001508 AtomicOrdering SuccessOrdering,
1509 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001510 SynchronizationScope SynchScope) {
1511 Op<0>() = Ptr;
1512 Op<1>() = Cmp;
1513 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001514 setSuccessOrdering(SuccessOrdering);
1515 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001516 setSynchScope(SynchScope);
1517
1518 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1519 "All operands must be non-null!");
1520 assert(getOperand(0)->getType()->isPointerTy() &&
1521 "Ptr must have pointer type!");
1522 assert(getOperand(1)->getType() ==
1523 cast<PointerType>(getOperand(0)->getType())->getElementType()
1524 && "Ptr must be a pointer to Cmp type!");
1525 assert(getOperand(2)->getType() ==
1526 cast<PointerType>(getOperand(0)->getType())->getElementType()
1527 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001528 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001529 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001530 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001531 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001532 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1533 "AtomicCmpXchg failure argument shall be no stronger than the success "
1534 "argument");
1535 assert(FailureOrdering != AtomicOrdering::Release &&
1536 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001537 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001538}
1539
1540AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001541 AtomicOrdering SuccessOrdering,
1542 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001543 SynchronizationScope SynchScope,
1544 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001545 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001546 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001547 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1548 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001549 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001550}
1551
1552AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001553 AtomicOrdering SuccessOrdering,
1554 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001555 SynchronizationScope SynchScope,
1556 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001557 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001558 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001559 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1560 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001561 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001562}
Tim Northover420a2162014-06-13 14:24:07 +00001563
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001564//===----------------------------------------------------------------------===//
1565// AtomicRMWInst Implementation
1566//===----------------------------------------------------------------------===//
1567
1568void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1569 AtomicOrdering Ordering,
1570 SynchronizationScope SynchScope) {
1571 Op<0>() = Ptr;
1572 Op<1>() = Val;
1573 setOperation(Operation);
1574 setOrdering(Ordering);
1575 setSynchScope(SynchScope);
1576
1577 assert(getOperand(0) && getOperand(1) &&
1578 "All operands must be non-null!");
1579 assert(getOperand(0)->getType()->isPointerTy() &&
1580 "Ptr must have pointer type!");
1581 assert(getOperand(1)->getType() ==
1582 cast<PointerType>(getOperand(0)->getType())->getElementType()
1583 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001584 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001585 "AtomicRMW instructions must be atomic!");
1586}
1587
1588AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1589 AtomicOrdering Ordering,
1590 SynchronizationScope SynchScope,
1591 Instruction *InsertBefore)
1592 : Instruction(Val->getType(), AtomicRMW,
1593 OperandTraits<AtomicRMWInst>::op_begin(this),
1594 OperandTraits<AtomicRMWInst>::operands(this),
1595 InsertBefore) {
1596 Init(Operation, Ptr, Val, Ordering, SynchScope);
1597}
1598
1599AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1600 AtomicOrdering Ordering,
1601 SynchronizationScope SynchScope,
1602 BasicBlock *InsertAtEnd)
1603 : Instruction(Val->getType(), AtomicRMW,
1604 OperandTraits<AtomicRMWInst>::op_begin(this),
1605 OperandTraits<AtomicRMWInst>::operands(this),
1606 InsertAtEnd) {
1607 Init(Operation, Ptr, Val, Ordering, SynchScope);
1608}
1609
1610//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001611// FenceInst Implementation
1612//===----------------------------------------------------------------------===//
1613
1614FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1615 SynchronizationScope SynchScope,
1616 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001617 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001618 setOrdering(Ordering);
1619 setSynchScope(SynchScope);
1620}
1621
1622FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1623 SynchronizationScope SynchScope,
1624 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001625 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001626 setOrdering(Ordering);
1627 setSynchScope(SynchScope);
1628}
1629
1630//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631// GetElementPtrInst Implementation
1632//===----------------------------------------------------------------------===//
1633
Pete Cooper1d078692015-12-14 20:29:16 +00001634void GetElementPtrInst::anchor() {}
1635
Jay Foadd1b78492011-07-25 09:48:08 +00001636void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001637 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001638 assert(getNumOperands() == 1 + IdxList.size() &&
1639 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001640 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001641 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001642 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001643}
1644
Gabor Greiff6caff662008-05-10 08:32:32 +00001645GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001646 : Instruction(GEPI.getType(), GetElementPtr,
1647 OperandTraits<GetElementPtrInst>::op_end(this) -
1648 GEPI.getNumOperands(),
1649 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001650 SourceElementType(GEPI.SourceElementType),
1651 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001652 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001653 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001654}
1655
Chris Lattner6090a422009-03-09 04:46:40 +00001656/// getIndexedType - Returns the type of the element that would be accessed with
1657/// a gep instruction with the specified parameters.
1658///
1659/// The Idxs pointer should point to a continuous piece of memory containing the
1660/// indices, either as Value* or uint64_t.
1661///
1662/// A null type is returned if the indices are invalid for the specified
1663/// pointer type.
1664///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001665template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001666static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001667 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001668 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001669 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001670
Chris Lattner6090a422009-03-09 04:46:40 +00001671 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001672 // it cannot be 'stepped over'.
1673 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001674 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001675
Dan Gohman1ecaf452008-05-31 00:58:22 +00001676 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001677 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001678 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001679 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001680 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001681 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001682 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001683 }
Craig Topperc6207612014-04-09 06:08:46 +00001684 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001685}
1686
David Blaikied288fb82015-03-30 21:41:43 +00001687Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1688 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001689}
1690
David Blaikied288fb82015-03-30 21:41:43 +00001691Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001692 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001693 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001694}
1695
David Blaikied288fb82015-03-30 21:41:43 +00001696Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1697 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001698}
1699
Chris Lattner45f15572007-04-14 00:12:57 +00001700/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1701/// zeros. If so, the result pointer and the first operand have the same
1702/// value, just potentially different types.
1703bool GetElementPtrInst::hasAllZeroIndices() const {
1704 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1705 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1706 if (!CI->isZero()) return false;
1707 } else {
1708 return false;
1709 }
1710 }
1711 return true;
1712}
1713
Chris Lattner27058292007-04-27 20:35:56 +00001714/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1715/// constant integers. If so, the result pointer and the first operand have
1716/// a constant offset between them.
1717bool GetElementPtrInst::hasAllConstantIndices() const {
1718 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1719 if (!isa<ConstantInt>(getOperand(i)))
1720 return false;
1721 }
1722 return true;
1723}
1724
Dan Gohman1b849082009-09-07 23:54:19 +00001725void GetElementPtrInst::setIsInBounds(bool B) {
1726 cast<GEPOperator>(this)->setIsInBounds(B);
1727}
Chris Lattner45f15572007-04-14 00:12:57 +00001728
Nick Lewycky28a5f252009-09-27 21:33:04 +00001729bool GetElementPtrInst::isInBounds() const {
1730 return cast<GEPOperator>(this)->isInBounds();
1731}
1732
Chandler Carruth1e140532012-12-11 10:29:10 +00001733bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1734 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001735 // Delegate to the generic GEPOperator implementation.
1736 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001737}
1738
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001739//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001740// ExtractElementInst Implementation
1741//===----------------------------------------------------------------------===//
1742
1743ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001744 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001745 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001746 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001747 ExtractElement,
1748 OperandTraits<ExtractElementInst>::op_begin(this),
1749 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001750 assert(isValidOperands(Val, Index) &&
1751 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001752 Op<0>() = Val;
1753 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001754 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001755}
1756
1757ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001758 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001759 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001760 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001761 ExtractElement,
1762 OperandTraits<ExtractElementInst>::op_begin(this),
1763 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001764 assert(isValidOperands(Val, Index) &&
1765 "Invalid extractelement instruction operands!");
1766
Gabor Greif2d3024d2008-05-26 21:33:52 +00001767 Op<0>() = Val;
1768 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001769 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001770}
1771
Chris Lattner65511ff2006-10-05 06:24:58 +00001772
Chris Lattner54865b32006-04-08 04:05:48 +00001773bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001774 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001775 return false;
1776 return true;
1777}
1778
1779
Robert Bocchino23004482006-01-10 19:05:34 +00001780//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001781// InsertElementInst Implementation
1782//===----------------------------------------------------------------------===//
1783
Chris Lattner54865b32006-04-08 04:05:48 +00001784InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001785 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001786 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001787 : Instruction(Vec->getType(), InsertElement,
1788 OperandTraits<InsertElementInst>::op_begin(this),
1789 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001790 assert(isValidOperands(Vec, Elt, Index) &&
1791 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001792 Op<0>() = Vec;
1793 Op<1>() = Elt;
1794 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001795 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001796}
1797
Chris Lattner54865b32006-04-08 04:05:48 +00001798InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001799 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001800 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001801 : Instruction(Vec->getType(), InsertElement,
1802 OperandTraits<InsertElementInst>::op_begin(this),
1803 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001804 assert(isValidOperands(Vec, Elt, Index) &&
1805 "Invalid insertelement instruction operands!");
1806
Gabor Greif2d3024d2008-05-26 21:33:52 +00001807 Op<0>() = Vec;
1808 Op<1>() = Elt;
1809 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001810 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001811}
1812
Chris Lattner54865b32006-04-08 04:05:48 +00001813bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1814 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001815 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001816 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001817
Reid Spencerd84d35b2007-02-15 02:26:10 +00001818 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001819 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001820
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001821 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001822 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001823 return true;
1824}
1825
1826
Robert Bocchinoca27f032006-01-17 20:07:22 +00001827//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001828// ShuffleVectorInst Implementation
1829//===----------------------------------------------------------------------===//
1830
1831ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001832 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001833 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001834: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001835 cast<VectorType>(Mask->getType())->getNumElements()),
1836 ShuffleVector,
1837 OperandTraits<ShuffleVectorInst>::op_begin(this),
1838 OperandTraits<ShuffleVectorInst>::operands(this),
1839 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001840 assert(isValidOperands(V1, V2, Mask) &&
1841 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001842 Op<0>() = V1;
1843 Op<1>() = V2;
1844 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001845 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001846}
1847
1848ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001849 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001850 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001851: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1852 cast<VectorType>(Mask->getType())->getNumElements()),
1853 ShuffleVector,
1854 OperandTraits<ShuffleVectorInst>::op_begin(this),
1855 OperandTraits<ShuffleVectorInst>::operands(this),
1856 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001857 assert(isValidOperands(V1, V2, Mask) &&
1858 "Invalid shuffle vector instruction operands!");
1859
Gabor Greif2d3024d2008-05-26 21:33:52 +00001860 Op<0>() = V1;
1861 Op<1>() = V2;
1862 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001863 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001864}
1865
Mon P Wang25f01062008-11-10 04:46:22 +00001866bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001867 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001868 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001869 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001870 return false;
1871
Chris Lattner1dcb6542012-01-25 23:49:49 +00001872 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001873 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001874 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001875 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001876
1877 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001878 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1879 return true;
1880
Sanjay Patel8bd52282017-04-19 16:22:19 +00001881 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001882 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001883 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001884 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001885 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001886 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001887 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001888 return false;
1889 }
1890 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001891 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001892 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001893
Sanjay Patel8bd52282017-04-19 16:22:19 +00001894 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001895 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1896 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1897 if (CDS->getElementAsInteger(i) >= V1Size*2)
1898 return false;
1899 return true;
1900 }
1901
1902 // The bitcode reader can create a place holder for a forward reference
1903 // used as the shuffle mask. When this occurs, the shuffle mask will
1904 // fall into this case and fail. To avoid this error, do this bit of
1905 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001906 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001907 if (CE->getOpcode() == Instruction::UserOp1)
1908 return true;
1909
1910 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001911}
1912
Chris Lattnercf129702012-01-26 02:51:13 +00001913int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1914 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001915 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001916 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001917 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001918 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001919 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001920 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001921}
1922
Chris Lattnercf129702012-01-26 02:51:13 +00001923void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1924 SmallVectorImpl<int> &Result) {
1925 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001926
Sanjay Patel8bd52282017-04-19 16:22:19 +00001927 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001928 for (unsigned i = 0; i != NumElts; ++i)
1929 Result.push_back(CDS->getElementAsInteger(i));
1930 return;
1931 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001932 for (unsigned i = 0; i != NumElts; ++i) {
1933 Constant *C = Mask->getAggregateElement(i);
1934 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001935 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001936 }
1937}
1938
1939
Dan Gohman12fce772008-05-15 19:50:34 +00001940//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001941// InsertValueInst Class
1942//===----------------------------------------------------------------------===//
1943
Jay Foad57aa6362011-07-13 10:26:04 +00001944void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1945 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001946 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001947
1948 // There's no fundamental reason why we require at least one index
1949 // (other than weirdness with &*IdxBegin being invalid; see
1950 // getelementptr's init routine for example). But there's no
1951 // present need to support it.
1952 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1953
1954 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001955 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001956 Op<0>() = Agg;
1957 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001958
Jay Foad57aa6362011-07-13 10:26:04 +00001959 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001960 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001961}
1962
1963InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001964 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001965 OperandTraits<InsertValueInst>::op_begin(this), 2),
1966 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001967 Op<0>() = IVI.getOperand(0);
1968 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001969 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001970}
1971
1972//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001973// ExtractValueInst Class
1974//===----------------------------------------------------------------------===//
1975
Jay Foad57aa6362011-07-13 10:26:04 +00001976void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001977 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001978
Jay Foad57aa6362011-07-13 10:26:04 +00001979 // There's no fundamental reason why we require at least one index.
1980 // But there's no present need to support it.
1981 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001982
Jay Foad57aa6362011-07-13 10:26:04 +00001983 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001984 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001985}
1986
1987ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001988 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001989 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001990 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001991}
1992
Dan Gohman12fce772008-05-15 19:50:34 +00001993// getIndexedType - Returns the type of the element that would be extracted
1994// with an extractvalue instruction with the specified parameters.
1995//
1996// A null type is returned if the indices are invalid for the specified
1997// pointer type.
1998//
Chris Lattner229907c2011-07-18 04:54:35 +00001999Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00002000 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00002001 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002002 // We can't use CompositeType::indexValid(Index) here.
2003 // indexValid() always returns true for arrays because getelementptr allows
2004 // out-of-bounds indices. Since we don't allow those for extractvalue and
2005 // insertvalue we need to check array indexing manually.
2006 // Since the only other types we can index into are struct types it's just
2007 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002008 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002009 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002010 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002011 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002012 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002013 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002014 } else {
2015 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002016 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002017 }
2018
2019 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002020 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002021 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002022}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002023
2024//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002025// BinaryOperator Class
2026//===----------------------------------------------------------------------===//
2027
Chris Lattner2195fc42007-02-24 00:55:48 +00002028BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002029 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002030 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002031 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002032 OperandTraits<BinaryOperator>::op_begin(this),
2033 OperandTraits<BinaryOperator>::operands(this),
2034 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002035 Op<0>() = S1;
2036 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002037 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002038 setName(Name);
2039}
2040
2041BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002042 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002043 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002044 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002045 OperandTraits<BinaryOperator>::op_begin(this),
2046 OperandTraits<BinaryOperator>::operands(this),
2047 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002048 Op<0>() = S1;
2049 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002050 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002051 setName(Name);
2052}
2053
2054
2055void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002056 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002057 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002058 assert(LHS->getType() == RHS->getType() &&
2059 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002060#ifndef NDEBUG
2061 switch (iType) {
2062 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002063 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002064 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002065 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002066 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002067 "Tried to create an integer operation on a non-integer type!");
2068 break;
2069 case FAdd: case FSub:
2070 case FMul:
2071 assert(getType() == LHS->getType() &&
2072 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002073 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002074 "Tried to create a floating-point operation on a "
2075 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002076 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002077 case UDiv:
2078 case SDiv:
2079 assert(getType() == LHS->getType() &&
2080 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002081 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002082 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002083 "Incorrect operand type (not integer) for S/UDIV");
2084 break;
2085 case FDiv:
2086 assert(getType() == LHS->getType() &&
2087 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002088 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002089 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002090 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002091 case URem:
2092 case SRem:
2093 assert(getType() == LHS->getType() &&
2094 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002095 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002096 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002097 "Incorrect operand type (not integer) for S/UREM");
2098 break;
2099 case FRem:
2100 assert(getType() == LHS->getType() &&
2101 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002102 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002103 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002104 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002105 case Shl:
2106 case LShr:
2107 case AShr:
2108 assert(getType() == LHS->getType() &&
2109 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002110 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002111 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002112 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002113 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002114 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002115 case And: case Or:
2116 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002117 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002118 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002119 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002120 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002121 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002122 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002123 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002124 default:
2125 break;
2126 }
2127#endif
2128}
2129
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002130BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002131 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002132 Instruction *InsertBefore) {
2133 assert(S1->getType() == S2->getType() &&
2134 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002135 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002136}
2137
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002138BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002139 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002140 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002141 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002142 InsertAtEnd->getInstList().push_back(Res);
2143 return Res;
2144}
2145
Dan Gohman5476cfd2009-08-12 16:23:25 +00002146BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002147 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002148 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002149 return new BinaryOperator(Instruction::Sub,
2150 zero, Op,
2151 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002152}
2153
Dan Gohman5476cfd2009-08-12 16:23:25 +00002154BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002155 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002156 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002157 return new BinaryOperator(Instruction::Sub,
2158 zero, Op,
2159 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002160}
2161
Dan Gohman4ab44202009-12-18 02:58:50 +00002162BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2163 Instruction *InsertBefore) {
2164 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2165 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2166}
2167
2168BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2169 BasicBlock *InsertAtEnd) {
2170 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2171 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2172}
2173
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002174BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2175 Instruction *InsertBefore) {
2176 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2177 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2178}
2179
2180BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2181 BasicBlock *InsertAtEnd) {
2182 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2183 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2184}
2185
Dan Gohman5476cfd2009-08-12 16:23:25 +00002186BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002187 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002188 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002189 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002190 Op->getType(), Name, InsertBefore);
2191}
2192
Dan Gohman5476cfd2009-08-12 16:23:25 +00002193BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002194 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002195 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002196 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002197 Op->getType(), Name, InsertAtEnd);
2198}
2199
Dan Gohman5476cfd2009-08-12 16:23:25 +00002200BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002201 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002202 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002203 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002204 Op->getType(), Name, InsertBefore);
2205}
2206
Dan Gohman5476cfd2009-08-12 16:23:25 +00002207BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002208 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002209 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002210 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002211 Op->getType(), Name, InsertAtEnd);
2212}
2213
2214
2215// isConstantAllOnes - Helper function for several functions below
2216static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002217 if (const Constant *C = dyn_cast<Constant>(V))
2218 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002219 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002220}
2221
Owen Andersonbb2501b2009-07-13 22:18:28 +00002222bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002223 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2224 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002225 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002226 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002227 return false;
2228}
2229
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002230bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002231 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2232 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002233 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002234 if (!IgnoreZeroSign)
2235 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2236 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2237 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002238 return false;
2239}
2240
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002241bool BinaryOperator::isNot(const Value *V) {
2242 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2243 return (Bop->getOpcode() == Instruction::Xor &&
2244 (isConstantAllOnes(Bop->getOperand(1)) ||
2245 isConstantAllOnes(Bop->getOperand(0))));
2246 return false;
2247}
2248
Chris Lattner2c7d1772005-04-24 07:28:37 +00002249Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002250 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002251}
2252
Chris Lattner2c7d1772005-04-24 07:28:37 +00002253const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2254 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002255}
2256
Dan Gohmana5b96452009-06-04 22:49:04 +00002257Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002258 return cast<BinaryOperator>(BinOp)->getOperand(1);
2259}
2260
2261const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2262 return getFNegArgument(const_cast<Value*>(BinOp));
2263}
2264
Chris Lattner2c7d1772005-04-24 07:28:37 +00002265Value *BinaryOperator::getNotArgument(Value *BinOp) {
2266 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2267 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2268 Value *Op0 = BO->getOperand(0);
2269 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002270 if (isConstantAllOnes(Op0)) return Op1;
2271
2272 assert(isConstantAllOnes(Op1));
2273 return Op0;
2274}
2275
Chris Lattner2c7d1772005-04-24 07:28:37 +00002276const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2277 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002278}
2279
2280
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002281// Exchange the two operands to this instruction. This instruction is safe to
2282// use on any binary instruction and does not modify the semantics of the
2283// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2284// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002285bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002286 if (!isCommutative())
2287 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002288 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002289 return false;
2290}
2291
Sanjay Patela982d992014-09-03 01:06:50 +00002292
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002293//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002294// FPMathOperator Class
2295//===----------------------------------------------------------------------===//
2296
Duncan Sands05f4df82012-04-16 16:28:59 +00002297float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002298 const MDNode *MD =
2299 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002300 if (!MD)
2301 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002302 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002303 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002304}
2305
2306
2307//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002308// CastInst Class
2309//===----------------------------------------------------------------------===//
2310
David Blaikie3a15e142011-12-01 08:00:17 +00002311void CastInst::anchor() {}
2312
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313// Just determine if this cast only deals with integral->integral conversion.
2314bool CastInst::isIntegerCast() const {
2315 switch (getOpcode()) {
2316 default: return false;
2317 case Instruction::ZExt:
2318 case Instruction::SExt:
2319 case Instruction::Trunc:
2320 return true;
2321 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002322 return getOperand(0)->getType()->isIntegerTy() &&
2323 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002325}
2326
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002327bool CastInst::isLosslessCast() const {
2328 // Only BitCast can be lossless, exit fast if we're not BitCast
2329 if (getOpcode() != Instruction::BitCast)
2330 return false;
2331
2332 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002333 Type *SrcTy = getOperand(0)->getType();
2334 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002335 if (SrcTy == DstTy)
2336 return true;
2337
Reid Spencer8d9336d2006-12-31 05:26:44 +00002338 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002339 if (SrcTy->isPointerTy())
2340 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002341 return false; // Other types have no identity values
2342}
2343
2344/// This function determines if the CastInst does not require any bits to be
2345/// changed in order to effect the cast. Essentially, it identifies cases where
2346/// no code gen is necessary for the cast, hence the name no-op cast. For
2347/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002348/// # bitcast i32* %x to i8*
2349/// # bitcast <2 x i32> %x to <4 x i16>
2350/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002351/// @brief Determine if the described cast is a no-op.
2352bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002353 Type *SrcTy,
2354 Type *DestTy,
2355 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002356 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002357 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358 case Instruction::Trunc:
2359 case Instruction::ZExt:
2360 case Instruction::SExt:
2361 case Instruction::FPTrunc:
2362 case Instruction::FPExt:
2363 case Instruction::UIToFP:
2364 case Instruction::SIToFP:
2365 case Instruction::FPToUI:
2366 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002367 case Instruction::AddrSpaceCast:
2368 // TODO: Target informations may give a more accurate answer here.
2369 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370 case Instruction::BitCast:
2371 return true; // BitCast never modifies bits.
2372 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002373 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002374 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002376 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002377 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 }
2379}
2380
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002381/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002382bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002383 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2384}
2385
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002386bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002387 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002388 if (getOpcode() == Instruction::PtrToInt)
2389 PtrOpTy = getOperand(0)->getType();
2390 else if (getOpcode() == Instruction::IntToPtr)
2391 PtrOpTy = getType();
2392
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002393 Type *IntPtrTy =
2394 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002395
2396 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2397}
2398
2399/// This function determines if a pair of casts can be eliminated and what
2400/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401/// instructions like this:
2402/// * %F = firstOpcode SrcTy %x to MidTy
2403/// * %S = secondOpcode MidTy %F to DstTy
2404/// The function returns a resultOpcode so these two casts can be replaced with:
2405/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002406/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407unsigned CastInst::isEliminableCastPair(
2408 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002409 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2410 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411 // Define the 144 possibilities for these two cast instructions. The values
2412 // in this matrix determine what to do in a given situation and select the
2413 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002414 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415 // the following cast properties:
2416 //
2417 // Size Compare Source Destination
2418 // Operator Src ? Size Type Sign Type Sign
2419 // -------- ------------ ------------------- ---------------------
2420 // TRUNC > Integer Any Integral Any
2421 // ZEXT < Integral Unsigned Integer Any
2422 // SEXT < Integral Signed Integer Any
2423 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002424 // FPTOSI n/a FloatPt n/a Integral Signed
2425 // UITOFP n/a Integral Unsigned FloatPt n/a
2426 // SITOFP n/a Integral Signed FloatPt n/a
2427 // FPTRUNC > FloatPt n/a FloatPt n/a
2428 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 // PTRTOINT n/a Pointer n/a Integral Unsigned
2430 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002431 // BITCAST = FirstClass n/a FirstClass n/a
2432 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002433 //
2434 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002435 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2436 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002437 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002438 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002439 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002440 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002441 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2443 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002444 // T F F U S F F P I B A -+
2445 // R Z S P P I I T P 2 N T S |
2446 // U E E 2 2 2 2 R E I T C C +- secondOp
2447 // N X X U S F F N X N 2 V V |
2448 // C T T I I P P C T T P T T -+
2449 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002450 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002451 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2452 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2453 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2454 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2455 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002456 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002457 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2458 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2459 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2460 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2461 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002463
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002464 // TODO: This logic could be encoded into the table above and handled in the
2465 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002466 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002467 // merging. However, any pair of bitcasts are allowed.
2468 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2469 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2470 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002471
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002472 // Check if any of the casts convert scalars <-> vectors.
2473 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2474 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2475 if (!AreBothBitcasts)
2476 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002477
2478 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2479 [secondOp-Instruction::CastOpsBegin];
2480 switch (ElimCase) {
2481 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002482 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483 return 0;
2484 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002485 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486 return firstOp;
2487 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002488 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002489 return secondOp;
2490 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002491 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002492 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002493 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002494 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495 return firstOp;
2496 return 0;
2497 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002498 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002499 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002500 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501 return firstOp;
2502 return 0;
2503 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002504 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002505 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002506 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 return secondOp;
2508 return 0;
2509 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002510 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002511 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002512 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513 return secondOp;
2514 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002515 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002516 // Cannot simplify if address spaces are different!
2517 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2518 return 0;
2519
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002520 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002521 // We can still fold this without knowing the actual sizes as long we
2522 // know that the intermediate pointer is the largest possible
2523 // pointer size.
2524 // FIXME: Is this always true?
2525 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002526 return Instruction::BitCast;
2527
2528 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002529 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002530 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002531 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532 if (MidSize >= PtrSize)
2533 return Instruction::BitCast;
2534 return 0;
2535 }
2536 case 8: {
2537 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2538 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2539 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002540 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2541 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542 if (SrcSize == DstSize)
2543 return Instruction::BitCast;
2544 else if (SrcSize < DstSize)
2545 return firstOp;
2546 return secondOp;
2547 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002548 case 9:
2549 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 return Instruction::ZExt;
2551 case 10:
2552 // fpext followed by ftrunc is allowed if the bit size returned to is
2553 // the same as the original, in which case its just a bitcast
2554 if (SrcTy == DstTy)
2555 return Instruction::BitCast;
2556 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002557 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002559 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002560 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002561 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002562 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2563 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564 if (SrcSize <= PtrSize && SrcSize == DstSize)
2565 return Instruction::BitCast;
2566 return 0;
2567 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002568 case 12: {
2569 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2570 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2571 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2572 return Instruction::AddrSpaceCast;
2573 return Instruction::BitCast;
2574 }
2575 case 13:
2576 // FIXME: this state can be merged with (1), but the following assert
2577 // is useful to check the correcteness of the sequence due to semantic
2578 // change of bitcast.
2579 assert(
2580 SrcTy->isPtrOrPtrVectorTy() &&
2581 MidTy->isPtrOrPtrVectorTy() &&
2582 DstTy->isPtrOrPtrVectorTy() &&
2583 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2584 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2585 "Illegal addrspacecast, bitcast sequence!");
2586 // Allowed, use first cast's opcode
2587 return firstOp;
2588 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002589 // bitcast, addrspacecast -> addrspacecast if the element type of
2590 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002591 if (SrcTy->getScalarType()->getPointerElementType() ==
2592 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002593 return Instruction::AddrSpaceCast;
2594 return 0;
2595
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002596 case 15:
2597 // FIXME: this state can be merged with (1), but the following assert
2598 // is useful to check the correcteness of the sequence due to semantic
2599 // change of bitcast.
2600 assert(
2601 SrcTy->isIntOrIntVectorTy() &&
2602 MidTy->isPtrOrPtrVectorTy() &&
2603 DstTy->isPtrOrPtrVectorTy() &&
2604 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2605 "Illegal inttoptr, bitcast sequence!");
2606 // Allowed, use first cast's opcode
2607 return firstOp;
2608 case 16:
2609 // FIXME: this state can be merged with (2), but the following assert
2610 // is useful to check the correcteness of the sequence due to semantic
2611 // change of bitcast.
2612 assert(
2613 SrcTy->isPtrOrPtrVectorTy() &&
2614 MidTy->isPtrOrPtrVectorTy() &&
2615 DstTy->isIntOrIntVectorTy() &&
2616 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2617 "Illegal bitcast, ptrtoint sequence!");
2618 // Allowed, use second cast's opcode
2619 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002620 case 17:
2621 // (sitofp (zext x)) -> (uitofp x)
2622 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002624 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002626 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002627 default:
Craig Topperc514b542012-02-05 22:14:15 +00002628 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630}
2631
Chris Lattner229907c2011-07-18 04:54:35 +00002632CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002633 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002634 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635 // Construct and return the appropriate CastInst subclass
2636 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002637 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2638 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2639 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2640 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2641 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2642 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2643 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2644 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2645 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2646 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2647 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2648 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2649 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2650 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002651 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652}
2653
Chris Lattner229907c2011-07-18 04:54:35 +00002654CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002655 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002656 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657 // Construct and return the appropriate CastInst subclass
2658 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002659 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2660 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2661 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2662 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2663 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2664 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2665 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2666 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2667 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2668 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2669 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2670 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2671 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2672 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002673 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674}
2675
Chris Lattner229907c2011-07-18 04:54:35 +00002676CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002677 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002678 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002679 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002680 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2681 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002682}
2683
Chris Lattner229907c2011-07-18 04:54:35 +00002684CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002685 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002686 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002687 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002688 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2689 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002690}
2691
Chris Lattner229907c2011-07-18 04:54:35 +00002692CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002693 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002694 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002695 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002696 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2697 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002698}
2699
Chris Lattner229907c2011-07-18 04:54:35 +00002700CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002701 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002702 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002703 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002704 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2705 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002706}
2707
Chris Lattner229907c2011-07-18 04:54:35 +00002708CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002709 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002710 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002711 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002712 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2713 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002714}
2715
Chris Lattner229907c2011-07-18 04:54:35 +00002716CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002717 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002718 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002719 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002720 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2721 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002722}
2723
Chris Lattner229907c2011-07-18 04:54:35 +00002724CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002725 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002726 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002727 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2728 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2729 "Invalid cast");
2730 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002731 assert((!Ty->isVectorTy() ||
2732 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002733 "Invalid cast");
2734
Matt Arsenault065ced92013-07-31 00:17:33 +00002735 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002736 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002737
Matt Arsenault740980e2014-07-14 17:24:35 +00002738 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002739}
2740
2741/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002742CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2743 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002744 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002745 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2746 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002747 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002748 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002749 assert((!Ty->isVectorTy() ||
2750 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002751 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002752
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002753 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002754 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002755
Matt Arsenault740980e2014-07-14 17:24:35 +00002756 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2757}
2758
2759CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2760 Value *S, Type *Ty,
2761 const Twine &Name,
2762 BasicBlock *InsertAtEnd) {
2763 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2764 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2765
2766 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2767 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2768
2769 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2770}
2771
2772CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2773 Value *S, Type *Ty,
2774 const Twine &Name,
2775 Instruction *InsertBefore) {
2776 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2777 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2778
2779 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002780 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2781
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002782 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002783}
2784
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002785CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2786 const Twine &Name,
2787 Instruction *InsertBefore) {
2788 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2789 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2790 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2791 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2792
2793 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2794}
2795
Matt Arsenault740980e2014-07-14 17:24:35 +00002796CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002797 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002798 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002799 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002800 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002801 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2802 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002803 Instruction::CastOps opcode =
2804 (SrcBits == DstBits ? Instruction::BitCast :
2805 (SrcBits > DstBits ? Instruction::Trunc :
2806 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002807 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002808}
2809
Chris Lattner229907c2011-07-18 04:54:35 +00002810CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002811 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002812 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002813 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002814 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002815 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2816 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002817 Instruction::CastOps opcode =
2818 (SrcBits == DstBits ? Instruction::BitCast :
2819 (SrcBits > DstBits ? Instruction::Trunc :
2820 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002821 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002822}
2823
Chris Lattner229907c2011-07-18 04:54:35 +00002824CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002825 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002826 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002827 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002828 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002829 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2830 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002831 Instruction::CastOps opcode =
2832 (SrcBits == DstBits ? Instruction::BitCast :
2833 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002834 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002835}
2836
Chris Lattner229907c2011-07-18 04:54:35 +00002837CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002838 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002839 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002840 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002841 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002842 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2843 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002844 Instruction::CastOps opcode =
2845 (SrcBits == DstBits ? Instruction::BitCast :
2846 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002847 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002848}
2849
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002850// Check whether it is valid to call getCastOpcode for these types.
2851// This routine must be kept in sync with getCastOpcode.
2852bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2853 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2854 return false;
2855
2856 if (SrcTy == DestTy)
2857 return true;
2858
2859 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2860 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2861 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2862 // An element by element cast. Valid if casting the elements is valid.
2863 SrcTy = SrcVecTy->getElementType();
2864 DestTy = DestVecTy->getElementType();
2865 }
2866
2867 // Get the bit sizes, we'll need these
2868 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2869 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2870
2871 // Run through the possibilities ...
2872 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002873 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002874 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002875 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002876 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002877 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002878 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002879 // Casting from something else
2880 return SrcTy->isPointerTy();
2881 }
2882 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2883 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002884 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002885 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002886 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002887 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002888 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002889 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002890 return false;
2891 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002892 if (DestTy->isVectorTy()) // Casting to vector
2893 return DestBits == SrcBits;
2894 if (DestTy->isPointerTy()) { // Casting to pointer
2895 if (SrcTy->isPointerTy()) // Casting from pointer
2896 return true;
2897 return SrcTy->isIntegerTy(); // Casting from integral
2898 }
2899 if (DestTy->isX86_MMXTy()) {
2900 if (SrcTy->isVectorTy())
2901 return DestBits == SrcBits; // 64-bit vector to MMX
2902 return false;
2903 } // Casting to something else
2904 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002905}
2906
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002907bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2908 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2909 return false;
2910
2911 if (SrcTy == DestTy)
2912 return true;
2913
2914 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2915 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2916 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2917 // An element by element cast. Valid if casting the elements is valid.
2918 SrcTy = SrcVecTy->getElementType();
2919 DestTy = DestVecTy->getElementType();
2920 }
2921 }
2922 }
2923
2924 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2925 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2926 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2927 }
2928 }
2929
2930 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2931 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2932
2933 // Could still have vectors of pointers if the number of elements doesn't
2934 // match
2935 if (SrcBits == 0 || DestBits == 0)
2936 return false;
2937
2938 if (SrcBits != DestBits)
2939 return false;
2940
2941 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2942 return false;
2943
2944 return true;
2945}
2946
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002947bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002948 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002949 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2950 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002951 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002952 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2953 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002954 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002955
2956 return isBitCastable(SrcTy, DestTy);
2957}
2958
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002959// Provide a way to get a "cast" where the cast opcode is inferred from the
2960// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002961// logic in the castIsValid function below. This axiom should hold:
2962// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2963// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002964// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002965// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002966Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002967CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002968 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2969 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002970
Duncan Sands55e50902008-01-06 10:12:28 +00002971 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2972 "Only first class types are castable!");
2973
Duncan Sandsa8514532011-05-18 07:13:41 +00002974 if (SrcTy == DestTy)
2975 return BitCast;
2976
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002977 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002978 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2979 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002980 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2981 // An element by element cast. Find the appropriate opcode based on the
2982 // element types.
2983 SrcTy = SrcVecTy->getElementType();
2984 DestTy = DestVecTy->getElementType();
2985 }
2986
2987 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002988 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2989 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002990
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002992 if (DestTy->isIntegerTy()) { // Casting to integral
2993 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002994 if (DestBits < SrcBits)
2995 return Trunc; // int -> smaller int
2996 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002997 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002998 return SExt; // signed -> SEXT
2999 else
3000 return ZExt; // unsigned -> ZEXT
3001 } else {
3002 return BitCast; // Same size, No-op cast
3003 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003004 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00003005 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003006 return FPToSI; // FP -> sint
3007 else
3008 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003009 } else if (SrcTy->isVectorTy()) {
3010 assert(DestBits == SrcBits &&
3011 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003012 return BitCast; // Same size, no-op cast
3013 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003014 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003015 "Casting from a value that is not first-class type");
3016 return PtrToInt; // ptr -> int
3017 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003018 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3019 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003020 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003021 return SIToFP; // sint -> FP
3022 else
3023 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003024 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003025 if (DestBits < SrcBits) {
3026 return FPTrunc; // FP -> smaller FP
3027 } else if (DestBits > SrcBits) {
3028 return FPExt; // FP -> larger FP
3029 } else {
3030 return BitCast; // same size, no-op cast
3031 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003032 } else if (SrcTy->isVectorTy()) {
3033 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003034 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003035 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003036 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003037 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003038 } else if (DestTy->isVectorTy()) {
3039 assert(DestBits == SrcBits &&
3040 "Illegal cast to vector (wrong type or size)");
3041 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003042 } else if (DestTy->isPointerTy()) {
3043 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003044 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3045 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003046 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003047 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003048 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003049 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003050 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003051 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003052 if (SrcTy->isVectorTy()) {
3053 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003054 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003055 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003056 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003057 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003058 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003059}
3060
3061//===----------------------------------------------------------------------===//
3062// CastInst SubClass Constructors
3063//===----------------------------------------------------------------------===//
3064
3065/// Check that the construction parameters for a CastInst are correct. This
3066/// could be broken out into the separate constructors but it is useful to have
3067/// it in one place and to eliminate the redundant code for getting the sizes
3068/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003069bool
Chris Lattner229907c2011-07-18 04:54:35 +00003070CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003071
3072 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003073 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003074
Chris Lattner37bc78a2010-01-26 21:51:43 +00003075 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3076 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003077 return false;
3078
3079 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003080 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3081 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003082
Duncan Sands7f646562011-05-18 09:21:57 +00003083 // If these are vector types, get the lengths of the vectors (using zero for
3084 // scalar types means that checking that vector lengths match also checks that
3085 // scalars are not being converted to vectors or vectors to scalars).
3086 unsigned SrcLength = SrcTy->isVectorTy() ?
3087 cast<VectorType>(SrcTy)->getNumElements() : 0;
3088 unsigned DstLength = DstTy->isVectorTy() ?
3089 cast<VectorType>(DstTy)->getNumElements() : 0;
3090
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003091 // Switch on the opcode provided
3092 switch (op) {
3093 default: return false; // This is an input error
3094 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003095 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3096 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003097 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003098 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3099 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003100 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003101 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3102 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003103 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003104 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3105 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003106 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003107 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3108 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003109 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003110 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003111 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3112 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003113 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003114 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003115 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3116 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003117 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003118 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003119 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003120 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3121 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3122 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003123 return SrcTy->getScalarType()->isPointerTy() &&
3124 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003125 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003126 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003127 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003128 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3129 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3130 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003131 return SrcTy->getScalarType()->isIntegerTy() &&
3132 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003133 case Instruction::BitCast: {
3134 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3135 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3136
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003137 // BitCast implies a no-op cast of type only. No bits change.
3138 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003139 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003140 return false;
3141
Alp Tokerf907b892013-12-05 05:44:44 +00003142 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003143 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003144 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003145 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3146
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003147 // If both are pointers then the address spaces must match.
3148 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3149 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003150
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003151 // A vector of pointers must have the same number of elements.
3152 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3153 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3154 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3155
3156 return false;
3157 }
3158
3159 return true;
3160 }
3161 case Instruction::AddrSpaceCast: {
3162 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3163 if (!SrcPtrTy)
3164 return false;
3165
3166 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3167 if (!DstPtrTy)
3168 return false;
3169
3170 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3171 return false;
3172
3173 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3174 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3175 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3176
3177 return false;
3178 }
3179
3180 return true;
3181 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003182 }
3183}
3184
3185TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003186 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003187) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003188 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003189}
3190
3191TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003192 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003193) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003194 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003195}
3196
3197ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003198 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003199) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003200 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003201}
3202
3203ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003204 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003205) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003206 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003207}
3208SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003209 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003210) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003211 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003212}
3213
Jeff Cohencc08c832006-12-02 02:22:01 +00003214SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003215 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003217 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003218}
3219
3220FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003221 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003223 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003224}
3225
3226FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003227 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003229 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003230}
3231
3232FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003233 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003235 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003236}
3237
3238FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003239 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003241 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003242}
3243
3244UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003245 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003246) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003247 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003248}
3249
3250UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003251 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003252) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003253 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003254}
3255
3256SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003257 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003258) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003259 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003260}
3261
3262SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003263 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003264) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003265 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003266}
3267
3268FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003269 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003270) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003271 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003272}
3273
3274FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003275 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003276) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003277 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003278}
3279
3280FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003281 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003282) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003283 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003284}
3285
3286FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003287 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003288) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003289 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003290}
3291
3292PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003293 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003295 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003296}
3297
3298PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003299 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003300) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003301 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003302}
3303
3304IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003305 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003306) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003307 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003308}
3309
3310IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003311 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003312) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003313 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003314}
3315
3316BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003317 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003318) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003319 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003320}
3321
3322BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003323 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003324) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003325 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003326}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003327
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003328AddrSpaceCastInst::AddrSpaceCastInst(
3329 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3330) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3332}
3333
3334AddrSpaceCastInst::AddrSpaceCastInst(
3335 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3336) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3337 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3338}
3339
Chris Lattnerf16dc002006-09-17 19:29:56 +00003340//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003341// CmpInst Classes
3342//===----------------------------------------------------------------------===//
3343
Craig Topper3186c012012-09-23 02:12:10 +00003344void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003345
Craig Topper1c3f2832015-12-15 06:11:33 +00003346CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3347 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003348 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003349 OperandTraits<CmpInst>::op_begin(this),
3350 OperandTraits<CmpInst>::operands(this),
3351 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003352 Op<0>() = LHS;
3353 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003354 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003355 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003356}
Gabor Greiff6caff662008-05-10 08:32:32 +00003357
Craig Topper1c3f2832015-12-15 06:11:33 +00003358CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3359 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003360 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003361 OperandTraits<CmpInst>::op_begin(this),
3362 OperandTraits<CmpInst>::operands(this),
3363 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003364 Op<0>() = LHS;
3365 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003366 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003367 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003368}
3369
3370CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003371CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003372 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003373 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003374 if (InsertBefore)
3375 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3376 S1, S2, Name);
3377 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003378 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003379 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003380 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003381
3382 if (InsertBefore)
3383 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3384 S1, S2, Name);
3385 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003386 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003387 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003388}
3389
3390CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003391CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003392 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003393 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003394 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3395 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003396 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003397 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3398 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003399}
3400
3401void CmpInst::swapOperands() {
3402 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3403 IC->swapOperands();
3404 else
3405 cast<FCmpInst>(this)->swapOperands();
3406}
3407
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003408bool CmpInst::isCommutative() const {
3409 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003410 return IC->isCommutative();
3411 return cast<FCmpInst>(this)->isCommutative();
3412}
3413
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003414bool CmpInst::isEquality() const {
3415 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003416 return IC->isEquality();
3417 return cast<FCmpInst>(this)->isEquality();
3418}
3419
3420
Dan Gohman4e724382008-05-31 02:47:54 +00003421CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003422 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003423 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003424 case ICMP_EQ: return ICMP_NE;
3425 case ICMP_NE: return ICMP_EQ;
3426 case ICMP_UGT: return ICMP_ULE;
3427 case ICMP_ULT: return ICMP_UGE;
3428 case ICMP_UGE: return ICMP_ULT;
3429 case ICMP_ULE: return ICMP_UGT;
3430 case ICMP_SGT: return ICMP_SLE;
3431 case ICMP_SLT: return ICMP_SGE;
3432 case ICMP_SGE: return ICMP_SLT;
3433 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003434
Dan Gohman4e724382008-05-31 02:47:54 +00003435 case FCMP_OEQ: return FCMP_UNE;
3436 case FCMP_ONE: return FCMP_UEQ;
3437 case FCMP_OGT: return FCMP_ULE;
3438 case FCMP_OLT: return FCMP_UGE;
3439 case FCMP_OGE: return FCMP_ULT;
3440 case FCMP_OLE: return FCMP_UGT;
3441 case FCMP_UEQ: return FCMP_ONE;
3442 case FCMP_UNE: return FCMP_OEQ;
3443 case FCMP_UGT: return FCMP_OLE;
3444 case FCMP_ULT: return FCMP_OGE;
3445 case FCMP_UGE: return FCMP_OLT;
3446 case FCMP_ULE: return FCMP_OGT;
3447 case FCMP_ORD: return FCMP_UNO;
3448 case FCMP_UNO: return FCMP_ORD;
3449 case FCMP_TRUE: return FCMP_FALSE;
3450 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003451 }
3452}
3453
Tim Northoverde3aea0412016-08-17 20:25:25 +00003454StringRef CmpInst::getPredicateName(Predicate Pred) {
3455 switch (Pred) {
3456 default: return "unknown";
3457 case FCmpInst::FCMP_FALSE: return "false";
3458 case FCmpInst::FCMP_OEQ: return "oeq";
3459 case FCmpInst::FCMP_OGT: return "ogt";
3460 case FCmpInst::FCMP_OGE: return "oge";
3461 case FCmpInst::FCMP_OLT: return "olt";
3462 case FCmpInst::FCMP_OLE: return "ole";
3463 case FCmpInst::FCMP_ONE: return "one";
3464 case FCmpInst::FCMP_ORD: return "ord";
3465 case FCmpInst::FCMP_UNO: return "uno";
3466 case FCmpInst::FCMP_UEQ: return "ueq";
3467 case FCmpInst::FCMP_UGT: return "ugt";
3468 case FCmpInst::FCMP_UGE: return "uge";
3469 case FCmpInst::FCMP_ULT: return "ult";
3470 case FCmpInst::FCMP_ULE: return "ule";
3471 case FCmpInst::FCMP_UNE: return "une";
3472 case FCmpInst::FCMP_TRUE: return "true";
3473 case ICmpInst::ICMP_EQ: return "eq";
3474 case ICmpInst::ICMP_NE: return "ne";
3475 case ICmpInst::ICMP_SGT: return "sgt";
3476 case ICmpInst::ICMP_SGE: return "sge";
3477 case ICmpInst::ICMP_SLT: return "slt";
3478 case ICmpInst::ICMP_SLE: return "sle";
3479 case ICmpInst::ICMP_UGT: return "ugt";
3480 case ICmpInst::ICMP_UGE: return "uge";
3481 case ICmpInst::ICMP_ULT: return "ult";
3482 case ICmpInst::ICMP_ULE: return "ule";
3483 }
3484}
3485
Pete Cooper1d078692015-12-14 20:29:16 +00003486void ICmpInst::anchor() {}
3487
Reid Spencer266e42b2006-12-23 06:05:41 +00003488ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3489 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003490 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003491 case ICMP_EQ: case ICMP_NE:
3492 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3493 return pred;
3494 case ICMP_UGT: return ICMP_SGT;
3495 case ICMP_ULT: return ICMP_SLT;
3496 case ICMP_UGE: return ICMP_SGE;
3497 case ICMP_ULE: return ICMP_SLE;
3498 }
3499}
3500
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003501ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3502 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003503 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003504 case ICMP_EQ: case ICMP_NE:
3505 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3506 return pred;
3507 case ICMP_SGT: return ICMP_UGT;
3508 case ICMP_SLT: return ICMP_ULT;
3509 case ICMP_SGE: return ICMP_UGE;
3510 case ICMP_SLE: return ICMP_ULE;
3511 }
3512}
3513
Dan Gohman4e724382008-05-31 02:47:54 +00003514CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003515 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003516 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003517 case ICMP_EQ: case ICMP_NE:
3518 return pred;
3519 case ICMP_SGT: return ICMP_SLT;
3520 case ICMP_SLT: return ICMP_SGT;
3521 case ICMP_SGE: return ICMP_SLE;
3522 case ICMP_SLE: return ICMP_SGE;
3523 case ICMP_UGT: return ICMP_ULT;
3524 case ICMP_ULT: return ICMP_UGT;
3525 case ICMP_UGE: return ICMP_ULE;
3526 case ICMP_ULE: return ICMP_UGE;
3527
Reid Spencerd9436b62006-11-20 01:22:35 +00003528 case FCMP_FALSE: case FCMP_TRUE:
3529 case FCMP_OEQ: case FCMP_ONE:
3530 case FCMP_UEQ: case FCMP_UNE:
3531 case FCMP_ORD: case FCMP_UNO:
3532 return pred;
3533 case FCMP_OGT: return FCMP_OLT;
3534 case FCMP_OLT: return FCMP_OGT;
3535 case FCMP_OGE: return FCMP_OLE;
3536 case FCMP_OLE: return FCMP_OGE;
3537 case FCMP_UGT: return FCMP_ULT;
3538 case FCMP_ULT: return FCMP_UGT;
3539 case FCMP_UGE: return FCMP_ULE;
3540 case FCMP_ULE: return FCMP_UGE;
3541 }
3542}
3543
Sanjoy Das6e78b172015-10-22 19:57:34 +00003544CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3545 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3546
3547 switch (pred) {
3548 default:
3549 llvm_unreachable("Unknown predicate!");
3550 case CmpInst::ICMP_ULT:
3551 return CmpInst::ICMP_SLT;
3552 case CmpInst::ICMP_ULE:
3553 return CmpInst::ICMP_SLE;
3554 case CmpInst::ICMP_UGT:
3555 return CmpInst::ICMP_SGT;
3556 case CmpInst::ICMP_UGE:
3557 return CmpInst::ICMP_SGE;
3558 }
3559}
3560
Craig Topper1c3f2832015-12-15 06:11:33 +00003561bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003562 switch (predicate) {
3563 default: return false;
3564 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3565 case ICmpInst::ICMP_UGE: return true;
3566 }
3567}
3568
Craig Topper1c3f2832015-12-15 06:11:33 +00003569bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003570 switch (predicate) {
3571 default: return false;
3572 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3573 case ICmpInst::ICMP_SGE: return true;
3574 }
3575}
3576
Craig Topper1c3f2832015-12-15 06:11:33 +00003577bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003578 switch (predicate) {
3579 default: return false;
3580 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3581 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3582 case FCmpInst::FCMP_ORD: return true;
3583 }
3584}
3585
Craig Topper1c3f2832015-12-15 06:11:33 +00003586bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003587 switch (predicate) {
3588 default: return false;
3589 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3590 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3591 case FCmpInst::FCMP_UNO: return true;
3592 }
3593}
3594
Craig Topper1c3f2832015-12-15 06:11:33 +00003595bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003596 switch(predicate) {
3597 default: return false;
3598 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3599 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3600 }
3601}
3602
Craig Topper1c3f2832015-12-15 06:11:33 +00003603bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003604 switch(predicate) {
3605 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3606 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3607 default: return false;
3608 }
3609}
3610
Chad Rosier99bc4802016-04-21 16:18:02 +00003611bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003612 // If the predicates match, then we know the first condition implies the
3613 // second is true.
3614 if (Pred1 == Pred2)
3615 return true;
3616
3617 switch (Pred1) {
3618 default:
3619 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003620 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003621 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier1a601592016-04-22 17:57:34 +00003622 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3623 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003624 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3625 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3626 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3627 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3628 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3629 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3630 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3631 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003632 }
3633 return false;
3634}
3635
Chad Rosier99bc4802016-04-21 16:18:02 +00003636bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003637 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003638}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003639
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003640//===----------------------------------------------------------------------===//
3641// SwitchInst Implementation
3642//===----------------------------------------------------------------------===//
3643
Chris Lattnerbaf00152010-11-17 05:41:46 +00003644void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3645 assert(Value && Default && NumReserved);
3646 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003647 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003648 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003649
Pete Coopereb31b682015-05-21 22:48:54 +00003650 Op<0>() = Value;
3651 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003652}
3653
Chris Lattner2195fc42007-02-24 00:55:48 +00003654/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3655/// switch on and a default destination. The number of additional cases can
3656/// be specified here to make memory allocation more efficient. This
3657/// constructor can also autoinsert before another instruction.
3658SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3659 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003660 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003661 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003662 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003663}
3664
3665/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3666/// switch on and a default destination. The number of additional cases can
3667/// be specified here to make memory allocation more efficient. This
3668/// constructor also autoinserts at the end of the specified BasicBlock.
3669SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3670 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003671 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003672 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003673 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003674}
3675
Misha Brukmanb1c93172005-04-21 23:48:37 +00003676SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003677 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003678 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003679 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003680 Use *OL = getOperandList();
3681 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003682 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003683 OL[i] = InOL[i];
3684 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003685 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003686 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003687}
3688
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003689
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003690/// addCase - Add an entry to the switch instruction...
3691///
Chris Lattner47ac1872005-02-24 05:32:09 +00003692void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003693 unsigned NewCaseIdx = getNumCases();
3694 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003695 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003696 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003697 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003698 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003699 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003700 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003701 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003702 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003703}
3704
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003705/// removeCase - This method removes the specified case and its successor
3706/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003707SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3708 unsigned idx = I->getCaseIndex();
3709
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003710 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003711
3712 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003713 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003714
Jay Foad14277722011-02-01 09:22:34 +00003715 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003716 if (2 + (idx + 1) * 2 != NumOps) {
3717 OL[2 + idx * 2] = OL[NumOps - 2];
3718 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003719 }
3720
3721 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003722 OL[NumOps-2].set(nullptr);
3723 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003724 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003725
3726 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003727}
3728
Jay Foade98f29d2011-04-01 08:00:58 +00003729/// growOperands - grow operands - This grows the operand list in response
3730/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003731///
Jay Foade98f29d2011-04-01 08:00:58 +00003732void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003733 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003734 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003735
3736 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003737 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003738}
3739
3740
3741BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3742 return getSuccessor(idx);
3743}
3744unsigned SwitchInst::getNumSuccessorsV() const {
3745 return getNumSuccessors();
3746}
3747void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3748 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003749}
Chris Lattnerf22be932004-10-15 23:52:53 +00003750
Chris Lattner3ed871f2009-10-27 19:13:16 +00003751//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003752// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003753//===----------------------------------------------------------------------===//
3754
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003755void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003756 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003757 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003758 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003759 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003760 allocHungoffUses(ReservedSpace);
3761
Pete Coopereb31b682015-05-21 22:48:54 +00003762 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003763}
3764
3765
Jay Foade98f29d2011-04-01 08:00:58 +00003766/// growOperands - grow operands - This grows the operand list in response
3767/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003768///
Jay Foade98f29d2011-04-01 08:00:58 +00003769void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003770 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003771 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003772
3773 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003774 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003775}
3776
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003777IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3778 Instruction *InsertBefore)
3779: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003780 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003781 init(Address, NumCases);
3782}
3783
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003784IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3785 BasicBlock *InsertAtEnd)
3786: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003787 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003788 init(Address, NumCases);
3789}
3790
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003791IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003792 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3793 nullptr, IBI.getNumOperands()) {
3794 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003795 Use *OL = getOperandList();
3796 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003797 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3798 OL[i] = InOL[i];
3799 SubclassOptionalData = IBI.SubclassOptionalData;
3800}
3801
Chris Lattner3ed871f2009-10-27 19:13:16 +00003802/// addDestination - Add a destination.
3803///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003804void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003805 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003806 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003807 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003808 // Initialize some new operands.
3809 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003810 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003811 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003812}
3813
3814/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003815/// indirectbr instruction.
3816void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003817 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3818
3819 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003820 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003821
3822 // Replace this value with the last one.
3823 OL[idx+1] = OL[NumOps-1];
3824
3825 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003826 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003827 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003828}
3829
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003830BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003831 return getSuccessor(idx);
3832}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003833unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003834 return getNumSuccessors();
3835}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003836void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003837 setSuccessor(idx, B);
3838}
3839
3840//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003841// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003842//===----------------------------------------------------------------------===//
3843
Chris Lattnerf22be932004-10-15 23:52:53 +00003844// Define these methods here so vtables don't get emitted into every translation
3845// unit that uses these classes.
3846
Pete Cooper75403d72015-06-24 20:22:23 +00003847GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003848 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003849}
3850
Pete Cooper75403d72015-06-24 20:22:23 +00003851BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003852 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003853}
3854
Pete Cooper75403d72015-06-24 20:22:23 +00003855FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003856 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003857}
3858
Pete Cooper75403d72015-06-24 20:22:23 +00003859ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003860 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003861}
3862
Pete Cooper75403d72015-06-24 20:22:23 +00003863ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003864 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003865}
3866
Pete Cooper75403d72015-06-24 20:22:23 +00003867InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003868 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003869}
3870
Pete Cooper75403d72015-06-24 20:22:23 +00003871AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003872 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003873 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003874 (Value *)getOperand(0), getAlignment());
3875 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003876 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003877 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003878}
3879
Pete Cooper75403d72015-06-24 20:22:23 +00003880LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003881 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3882 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003883}
3884
Pete Cooper75403d72015-06-24 20:22:23 +00003885StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003886 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003887 getAlignment(), getOrdering(), getSynchScope());
3888
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003892 AtomicCmpXchgInst *Result =
3893 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003894 getSuccessOrdering(), getFailureOrdering(),
3895 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003896 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003897 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003898 return Result;
3899}
3900
Pete Cooper75403d72015-06-24 20:22:23 +00003901AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003902 AtomicRMWInst *Result =
3903 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3904 getOrdering(), getSynchScope());
3905 Result->setVolatile(isVolatile());
3906 return Result;
3907}
3908
Pete Cooper75403d72015-06-24 20:22:23 +00003909FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003910 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3911}
3912
Pete Cooper75403d72015-06-24 20:22:23 +00003913TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003914 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003915}
3916
Pete Cooper75403d72015-06-24 20:22:23 +00003917ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003918 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003919}
3920
Pete Cooper75403d72015-06-24 20:22:23 +00003921SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003922 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003923}
3924
Pete Cooper75403d72015-06-24 20:22:23 +00003925FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003926 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003927}
3928
Pete Cooper75403d72015-06-24 20:22:23 +00003929FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003930 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003931}
3932
Pete Cooper75403d72015-06-24 20:22:23 +00003933UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003934 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003935}
3936
Pete Cooper75403d72015-06-24 20:22:23 +00003937SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003938 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003939}
3940
Pete Cooper75403d72015-06-24 20:22:23 +00003941FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003942 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003943}
3944
Pete Cooper75403d72015-06-24 20:22:23 +00003945FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003946 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003947}
3948
Pete Cooper75403d72015-06-24 20:22:23 +00003949PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003950 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003951}
3952
Pete Cooper75403d72015-06-24 20:22:23 +00003953IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003954 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003955}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003956
Pete Cooper75403d72015-06-24 20:22:23 +00003957BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003958 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003959}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003960
Pete Cooper75403d72015-06-24 20:22:23 +00003961AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003962 return new AddrSpaceCastInst(getOperand(0), getType());
3963}
3964
Pete Cooper75403d72015-06-24 20:22:23 +00003965CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003966 if (hasOperandBundles()) {
3967 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3968 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3969 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003970 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003971}
3972
Pete Cooper75403d72015-06-24 20:22:23 +00003973SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003974 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003975}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003976
Pete Cooper75403d72015-06-24 20:22:23 +00003977VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003978 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003979}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003980
Pete Cooper75403d72015-06-24 20:22:23 +00003981ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003982 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003983}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003984
Pete Cooper75403d72015-06-24 20:22:23 +00003985InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003986 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003987}
3988
Pete Cooper75403d72015-06-24 20:22:23 +00003989ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003990 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003991}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003992
Pete Cooper75403d72015-06-24 20:22:23 +00003993PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003994
Pete Cooper75403d72015-06-24 20:22:23 +00003995LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003996 return new LandingPadInst(*this);
3997}
3998
Pete Cooper75403d72015-06-24 20:22:23 +00003999ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004000 return new(getNumOperands()) ReturnInst(*this);
4001}
4002
Pete Cooper75403d72015-06-24 20:22:23 +00004003BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004004 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004005}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004006
Pete Cooper75403d72015-06-24 20:22:23 +00004007SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004008
Pete Cooper75403d72015-06-24 20:22:23 +00004009IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004010 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004011}
4012
Pete Cooper75403d72015-06-24 20:22:23 +00004013InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004014 if (hasOperandBundles()) {
4015 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4016 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4017 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004018 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004019}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004020
Pete Cooper75403d72015-06-24 20:22:23 +00004021ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004022
David Majnemer654e1302015-07-31 17:58:14 +00004023CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4024 return new (getNumOperands()) CleanupReturnInst(*this);
4025}
4026
David Majnemer654e1302015-07-31 17:58:14 +00004027CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004028 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004029}
4030
David Majnemer8a1c45d2015-12-12 05:38:55 +00004031CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4032 return new CatchSwitchInst(*this);
4033}
4034
4035FuncletPadInst *FuncletPadInst::cloneImpl() const {
4036 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004037}
4038
Pete Cooper75403d72015-06-24 20:22:23 +00004039UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004040 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004041 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004042}