blob: d7baa9ebc22330d314b27a4f15d6331e3b8c747b [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +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
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000016#include "llvm/ADT/None.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/IR/Attributes.h"
20#include "llvm/IR/BasicBlock.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000021#include "llvm/IR/CallSite.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000022#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000027#include "llvm/IR/InstrTypes.h"
28#include "llvm/IR/Instruction.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
36#include "llvm/Support/AtomicOrdering.h"
37#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000039#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000040#include <algorithm>
41#include <cassert>
42#include <cstdint>
43#include <vector>
44
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000045using namespace llvm;
46
Chris Lattner3e13b8c2008-01-02 23:42:30 +000047//===----------------------------------------------------------------------===//
48// CallSite Class
49//===----------------------------------------------------------------------===//
50
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000051User::op_iterator CallSite::getCallee() const {
52 Instruction *II(getInstruction());
53 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000054 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000055 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000056}
57
Gordon Henriksen14a55692007-12-10 02:14:30 +000058//===----------------------------------------------------------------------===//
59// TerminatorInst Class
60//===----------------------------------------------------------------------===//
61
Reid Kleckner45a13e12017-05-11 21:26:55 +000062unsigned TerminatorInst::getNumSuccessors() const {
63 switch (getOpcode()) {
64#define HANDLE_TERM_INST(N, OPC, CLASS) \
65 case Instruction::OPC: \
66 return static_cast<const CLASS *>(this)->getNumSuccessorsV();
67#include "llvm/IR/Instruction.def"
68 default:
69 break;
70 }
71 llvm_unreachable("not a terminator");
72}
73
74BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const {
75 switch (getOpcode()) {
76#define HANDLE_TERM_INST(N, OPC, CLASS) \
77 case Instruction::OPC: \
78 return static_cast<const CLASS *>(this)->getSuccessorV(idx);
79#include "llvm/IR/Instruction.def"
80 default:
81 break;
82 }
83 llvm_unreachable("not a terminator");
84}
85
86void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) {
87 switch (getOpcode()) {
88#define HANDLE_TERM_INST(N, OPC, CLASS) \
89 case Instruction::OPC: \
90 return static_cast<CLASS *>(this)->setSuccessorV(idx, B);
91#include "llvm/IR/Instruction.def"
92 default:
93 break;
94 }
95 llvm_unreachable("not a terminator");
96}
97
Gabor Greiff6caff662008-05-10 08:32:32 +000098//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000099// SelectInst Class
100//===----------------------------------------------------------------------===//
101
102/// areInvalidOperands - Return a string if the specified operands are invalid
103/// for a select operation, otherwise return null.
104const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
105 if (Op1->getType() != Op2->getType())
106 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +0000107
108 if (Op1->getType()->isTokenTy())
109 return "select values cannot have token type";
110
Chris Lattner229907c2011-07-18 04:54:35 +0000111 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +0000112 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000113 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000114 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +0000115 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +0000116 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +0000117 return "selected values for vector select must be vectors";
118 if (ET->getNumElements() != VT->getNumElements())
119 return "vector select requires selected vectors to have "
120 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000121 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000122 return "select condition must be i1 or <n x i1>";
123 }
Craig Topperc6207612014-04-09 06:08:46 +0000124 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +0000125}
126
Chris Lattner88107952008-12-29 00:12:50 +0000127//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128// PHINode Class
129//===----------------------------------------------------------------------===//
130
131PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000132 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
133 ReservedSpace(PN.getNumOperands()) {
134 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000135 std::copy(PN.op_begin(), PN.op_end(), op_begin());
136 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000137 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140// removeIncomingValue - Remove an incoming value. This is useful if a
141// predecessor basic block is deleted.
142Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000143 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144
145 // Move everything after this operand down.
146 //
147 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000148 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000150 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
151 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000152
153 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000154 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000155 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156
157 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000158 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000160 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161 eraseFromParent();
162 }
163 return Removed;
164}
165
Jay Foade98f29d2011-04-01 08:00:58 +0000166/// growOperands - grow operands - This grows the operand list in response
167/// to a push_back style of operation. This grows the number of ops by 1.5
168/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000169///
Jay Foade98f29d2011-04-01 08:00:58 +0000170void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000171 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000172 unsigned NumOps = e + e / 2;
173 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
174
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000175 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000176 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000177}
178
Nate Begemanb3923212005-08-04 23:24:19 +0000179/// hasConstantValue - If the specified PHI node always merges together the same
180/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000181Value *PHINode::hasConstantValue() const {
182 // Exploit the fact that phi nodes always have at least one entry.
183 Value *ConstantValue = getIncomingValue(0);
184 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000185 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
186 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000187 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000188 // The case where the first value is this PHI.
189 ConstantValue = getIncomingValue(i);
190 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000191 if (ConstantValue == this)
192 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000193 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000194}
195
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000196/// hasConstantOrUndefValue - Whether the specified PHI node always merges
197/// together the same value, assuming that undefs result in the same value as
198/// non-undefs.
199/// Unlike \ref hasConstantValue, this does not return a value because the
200/// unique non-undef incoming value need not dominate the PHI node.
201bool PHINode::hasConstantOrUndefValue() const {
202 Value *ConstantValue = nullptr;
203 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
204 Value *Incoming = getIncomingValue(i);
205 if (Incoming != this && !isa<UndefValue>(Incoming)) {
206 if (ConstantValue && ConstantValue != Incoming)
207 return false;
208 ConstantValue = Incoming;
209 }
210 }
211 return true;
212}
213
Bill Wendlingfae14752011-08-12 20:24:12 +0000214//===----------------------------------------------------------------------===//
215// LandingPadInst Implementation
216//===----------------------------------------------------------------------===//
217
David Majnemer7fddecc2015-06-17 20:52:32 +0000218LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
219 const Twine &NameStr, Instruction *InsertBefore)
220 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
221 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000222}
223
David Majnemer7fddecc2015-06-17 20:52:32 +0000224LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
225 const Twine &NameStr, BasicBlock *InsertAtEnd)
226 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
227 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000228}
229
230LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000231 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
232 LP.getNumOperands()),
233 ReservedSpace(LP.getNumOperands()) {
234 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000235 Use *OL = getOperandList();
236 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000237 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
238 OL[I] = InOL[I];
239
240 setCleanup(LP.isCleanup());
241}
242
David Majnemer7fddecc2015-06-17 20:52:32 +0000243LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000244 const Twine &NameStr,
245 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000246 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000247}
248
David Majnemer7fddecc2015-06-17 20:52:32 +0000249LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000250 const Twine &NameStr,
251 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000252 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000253}
254
David Majnemer7fddecc2015-06-17 20:52:32 +0000255void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000256 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000257 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000258 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000259 setName(NameStr);
260 setCleanup(false);
261}
262
263/// growOperands - grow operands - This grows the operand list in response to a
264/// push_back style of operation. This grows the number of ops by 2 times.
265void LandingPadInst::growOperands(unsigned Size) {
266 unsigned e = getNumOperands();
267 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000268 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000269 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000270}
271
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000272void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000273 unsigned OpNo = getNumOperands();
274 growOperands(1);
275 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000276 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000277 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000278}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279
280//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281// CallInst Implementation
282//===----------------------------------------------------------------------===//
283
David Blaikie348de692015-04-23 21:36:23 +0000284void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000285 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000286 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000287 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
288 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000289 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290
Jay Foad5bd375a2011-07-15 08:37:34 +0000291#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000292 assert((Args.size() == FTy->getNumParams() ||
293 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000294 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000295
296 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000297 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000298 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000299 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000300#endif
301
302 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000303
304 auto It = populateBundleOperandInfos(Bundles, Args.size());
305 (void)It;
306 assert(It + 1 == op_end() && "Should add up!");
307
Jay Foad5bd375a2011-07-15 08:37:34 +0000308 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
Jay Foad5bd375a2011-07-15 08:37:34 +0000311void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000312 FTy =
313 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000314 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000315 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000316
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000317 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000318
319 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
321
Daniel Dunbar4975db62009-07-25 04:41:11 +0000322CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 Instruction *InsertBefore)
324 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
325 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000326 Instruction::Call,
327 OperandTraits<CallInst>::op_end(this) - 1,
328 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000329 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330}
331
Daniel Dunbar4975db62009-07-25 04:41:11 +0000332CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 BasicBlock *InsertAtEnd)
334 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
335 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000336 Instruction::Call,
337 OperandTraits<CallInst>::op_end(this) - 1,
338 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000339 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
341
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000343 : Instruction(CI.getType(), Instruction::Call,
344 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
345 CI.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000346 Attrs(CI.Attrs), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000347 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000348 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000349
Jay Foad5bd375a2011-07-15 08:37:34 +0000350 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000351 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
352 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000353 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354}
355
Sanjoy Das2d161452015-11-18 06:23:38 +0000356CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
357 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000358 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000359
360 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
361 InsertPt);
362 NewCI->setTailCallKind(CI->getTailCallKind());
363 NewCI->setCallingConv(CI->getCallingConv());
364 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000365 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000366 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000367 return NewCI;
368}
369
Hal Finkele87ad542016-07-10 23:01:32 +0000370Value *CallInst::getReturnedArgOperand() const {
371 unsigned Index;
372
Reid Klecknerb5180542017-03-21 16:57:19 +0000373 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000374 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000375 if (const Function *F = getCalledFunction())
376 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
377 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000378 return getArgOperand(Index - AttributeList::FirstArgIndex);
379
Hal Finkele87ad542016-07-10 23:01:32 +0000380 return nullptr;
381}
382
Amaury Sechet392638d2016-06-14 20:27:35 +0000383void CallInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000384 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000385 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000386 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000387}
388
Amaury Secheta65a2372016-06-15 05:14:29 +0000389void CallInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000390 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000391 PAL = PAL.addAttribute(getContext(), i, Attr);
392 setAttributes(PAL);
393}
394
Reid Klecknera0b45f42017-05-03 18:17:31 +0000395void CallInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
396 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
397}
398
Amaury Sechet392638d2016-06-14 20:27:35 +0000399void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000400 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000401 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000402 setAttributes(PAL);
403}
404
Amaury Sechet6100adf2016-06-15 17:50:39 +0000405void CallInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000406 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000407 PAL = PAL.removeAttribute(getContext(), i, Kind);
408 setAttributes(PAL);
409}
410
Reid Klecknera0b45f42017-05-03 18:17:31 +0000411void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
412 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
413}
414
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000415void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000416 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000417 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
418 setAttributes(PAL);
419}
420
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000421void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000422 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000423 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
424 setAttributes(PAL);
425}
426
Reid Klecknerfb502d22017-04-14 20:19:02 +0000427bool CallInst::hasRetAttr(Attribute::AttrKind Kind) const {
428 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
429 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000430
Reid Klecknerfb502d22017-04-14 20:19:02 +0000431 // Look at the callee, if available.
432 if (const Function *F = getCalledFunction())
433 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
434 return false;
435}
436
437bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
438 assert(i < getNumArgOperands() && "Param index out of bounds!");
439
440 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000441 return true;
442 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000443 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000444 return false;
445}
446
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000447bool CallInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000448 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000449 // There are getNumOperands() - 1 data operands. The last operand is the
450 // callee.
451 assert(i < getNumOperands() && "Data operand index out of bounds!");
452
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000453 // The attribute A can either be directly specified, if the operand in
454 // question is a call argument; or be indirectly implied by the kind of its
455 // containing operand bundle, if the operand is a bundle operand.
456
Reid Kleckner545aa4f2017-05-23 17:03:28 +0000457 if (i == AttributeList::ReturnIndex)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000458 return hasRetAttr(Kind);
459
Reid Klecknerfb502d22017-04-14 20:19:02 +0000460 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
461 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000462 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000463 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000464
465 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
466 "Must be either a call argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000467 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000468}
469
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000470/// IsConstantOne - Return true only if val is constant int 1
471static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000472 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000473 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
474 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000475}
476
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000477static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000478 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000479 Type *AllocTy, Value *AllocSize,
480 Value *ArraySize,
481 ArrayRef<OperandBundleDef> OpB,
482 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000483 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000484 "createMalloc needs either InsertBefore or InsertAtEnd");
485
486 // malloc(type) becomes:
487 // bitcast (i8* malloc(typeSize)) to type*
488 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000489 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000490 if (!ArraySize)
491 ArraySize = ConstantInt::get(IntPtrTy, 1);
492 else if (ArraySize->getType() != IntPtrTy) {
493 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000494 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
495 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000496 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000497 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
498 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000499 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000500
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000501 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000502 if (IsConstantOne(AllocSize)) {
503 AllocSize = ArraySize; // Operand * 1 = Operand
504 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
505 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
506 false /*ZExt*/);
507 // Malloc arg is constant product of type size and array size
508 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
509 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000510 // Multiply type size by the array size...
511 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000512 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
513 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000514 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000515 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
516 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000517 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000518 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000519
Victor Hernandez788eaab2009-09-18 19:20:02 +0000520 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000521 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000522 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
523 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000524 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000525 Value *MallocFunc = MallocF;
526 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000527 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000528 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000529 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000530 CallInst *MCall = nullptr;
531 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000532 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000533 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
534 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000535 Result = MCall;
536 if (Result->getType() != AllocPtrType)
537 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000538 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000539 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000540 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000541 Result = MCall;
542 if (Result->getType() != AllocPtrType) {
543 InsertAtEnd->getInstList().push_back(MCall);
544 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000545 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000546 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000547 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000548 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000549 if (Function *F = dyn_cast<Function>(MallocFunc)) {
550 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000551 if (!F->returnDoesNotAlias())
552 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000553 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000554 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000555
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000556 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000557}
558
559/// CreateMalloc - Generate the IR for a call to malloc:
560/// 1. Compute the malloc call's argument as the specified type's size,
561/// possibly multiplied by the array size if the array size is not
562/// constant 1.
563/// 2. Call malloc with that argument.
564/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000565Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000566 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000567 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000568 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000569 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000570 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000571 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000572}
David Majnemerfadc6db2016-04-29 08:07:22 +0000573Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
574 Type *IntPtrTy, Type *AllocTy,
575 Value *AllocSize, Value *ArraySize,
576 ArrayRef<OperandBundleDef> OpB,
577 Function *MallocF,
578 const Twine &Name) {
579 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
580 ArraySize, OpB, MallocF, Name);
581}
582
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000583/// CreateMalloc - Generate the IR for a call to malloc:
584/// 1. Compute the malloc call's argument as the specified type's size,
585/// possibly multiplied by the array size if the array size is not
586/// constant 1.
587/// 2. Call malloc with that argument.
588/// 3. Bitcast the result of the malloc call to the specified type.
589/// Note: This function does not add the bitcast to the basic block, that is the
590/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000591Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000592 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000593 Value *AllocSize, Value *ArraySize,
594 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000595 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000596 ArraySize, None, MallocF, Name);
597}
598Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
599 Type *IntPtrTy, Type *AllocTy,
600 Value *AllocSize, Value *ArraySize,
601 ArrayRef<OperandBundleDef> OpB,
602 Function *MallocF, const Twine &Name) {
603 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
604 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000605}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000606
David Majnemerfadc6db2016-04-29 08:07:22 +0000607static Instruction *createFree(Value *Source,
608 ArrayRef<OperandBundleDef> Bundles,
609 Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000610 BasicBlock *InsertAtEnd) {
611 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
612 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000613 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000614 "Can not free something of nonpointer type!");
615
Ana Pazosb3596022016-02-03 21:34:39 +0000616 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
617 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000618
Chris Lattner229907c2011-07-18 04:54:35 +0000619 Type *VoidTy = Type::getVoidTy(M->getContext());
620 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000621 // prototype free as "void free(void*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000622 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
Ana Pazosb3596022016-02-03 21:34:39 +0000623 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000624 Value *PtrCast = Source;
625 if (InsertBefore) {
626 if (Source->getType() != IntPtrTy)
627 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
David Majnemerfadc6db2016-04-29 08:07:22 +0000628 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000629 } else {
630 if (Source->getType() != IntPtrTy)
631 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000632 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000633 }
634 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000635 if (Function *F = dyn_cast<Function>(FreeFunc))
636 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000637
638 return Result;
639}
640
641/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000642Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000643 return createFree(Source, None, InsertBefore, nullptr);
644}
645Instruction *CallInst::CreateFree(Value *Source,
646 ArrayRef<OperandBundleDef> Bundles,
647 Instruction *InsertBefore) {
648 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000649}
650
651/// CreateFree - Generate the IR for a call to the builtin free function.
652/// Note: This function does not add the call to the basic block, that is the
653/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000654Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000655 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
656 assert(FreeCall && "CreateFree did not create a CallInst");
657 return FreeCall;
658}
659Instruction *CallInst::CreateFree(Value *Source,
660 ArrayRef<OperandBundleDef> Bundles,
661 BasicBlock *InsertAtEnd) {
662 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000663 assert(FreeCall && "CreateFree did not create a CallInst");
664 return FreeCall;
665}
666
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000667//===----------------------------------------------------------------------===//
668// InvokeInst Implementation
669//===----------------------------------------------------------------------===//
670
David Blaikie3e807092015-05-13 18:35:26 +0000671void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
672 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000673 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000674 const Twine &NameStr) {
675 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000676
Sanjoy Das9303c242015-09-24 19:14:18 +0000677 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
678 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000679 Op<-3>() = Fn;
680 Op<-2>() = IfNormal;
681 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000682
683#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000684 assert(((Args.size() == FTy->getNumParams()) ||
685 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000686 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000687
Jay Foad5bd375a2011-07-15 08:37:34 +0000688 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000689 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000690 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000691 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000692#endif
693
694 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000695
696 auto It = populateBundleOperandInfos(Bundles, Args.size());
697 (void)It;
698 assert(It + 3 == op_end() && "Should add up!");
699
Jay Foad5bd375a2011-07-15 08:37:34 +0000700 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000701}
702
Misha Brukmanb1c93172005-04-21 23:48:37 +0000703InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000704 : TerminatorInst(II.getType(), Instruction::Invoke,
705 OperandTraits<InvokeInst>::op_end(this) -
706 II.getNumOperands(),
707 II.getNumOperands()),
Reid Klecknerb5180542017-03-21 16:57:19 +0000708 Attrs(II.Attrs), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000709 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000710 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000711 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
712 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000713 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714}
715
Sanjoy Das2d161452015-11-18 06:23:38 +0000716InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
717 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000718 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000719
720 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
721 II->getUnwindDest(), Args, OpB,
722 II->getName(), InsertPt);
723 NewII->setCallingConv(II->getCallingConv());
724 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000725 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000726 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000727 return NewII;
728}
729
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
731 return getSuccessor(idx);
732}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000733
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734unsigned InvokeInst::getNumSuccessorsV() const {
735 return getNumSuccessors();
736}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000737
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000738void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
739 return setSuccessor(idx, B);
740}
741
Hal Finkele87ad542016-07-10 23:01:32 +0000742Value *InvokeInst::getReturnedArgOperand() const {
743 unsigned Index;
744
Reid Klecknerb5180542017-03-21 16:57:19 +0000745 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000746 return getArgOperand(Index - AttributeList::FirstArgIndex);
Hal Finkele87ad542016-07-10 23:01:32 +0000747 if (const Function *F = getCalledFunction())
748 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
749 Index)
Reid Klecknera0b45f42017-05-03 18:17:31 +0000750 return getArgOperand(Index - AttributeList::FirstArgIndex);
751
Hal Finkele87ad542016-07-10 23:01:32 +0000752 return nullptr;
753}
754
Reid Klecknerfb502d22017-04-14 20:19:02 +0000755bool InvokeInst::hasRetAttr(Attribute::AttrKind Kind) const {
756 if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind))
757 return true;
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000758
Reid Klecknerfb502d22017-04-14 20:19:02 +0000759 // Look at the callee, if available.
760 if (const Function *F = getCalledFunction())
761 return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind);
762 return false;
763}
764
765bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
766 assert(i < getNumArgOperands() && "Param index out of bounds!");
767
768 if (Attrs.hasParamAttribute(i, Kind))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000769 return true;
770 if (const Function *F = getCalledFunction())
Reid Klecknerfb502d22017-04-14 20:19:02 +0000771 return F->getAttributes().hasParamAttribute(i, Kind);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000772 return false;
773}
774
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000775bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
Amaury Sechet392638d2016-06-14 20:27:35 +0000776 Attribute::AttrKind Kind) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000777 // There are getNumOperands() - 3 data operands. The last three operands are
778 // the callee and the two successor basic blocks.
779 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
780
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000781 // The attribute A can either be directly specified, if the operand in
782 // question is an invoke argument; or be indirectly implied by the kind of its
783 // containing operand bundle, if the operand is a bundle operand.
784
Reid Kleckner545aa4f2017-05-23 17:03:28 +0000785 if (i == AttributeList::ReturnIndex)
Reid Kleckner8bf67fe2017-05-23 17:01:48 +0000786 return hasRetAttr(Kind);
787
Reid Klecknerfb502d22017-04-14 20:19:02 +0000788 // FIXME: Avoid these i - 1 calculations and update the API to use zero-based
789 // indices.
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000790 if (i < (getNumArgOperands() + 1))
Reid Klecknerfb502d22017-04-14 20:19:02 +0000791 return paramHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000792
793 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
794 "Must be either an invoke argument or an operand bundle!");
Amaury Sechet392638d2016-06-14 20:27:35 +0000795 return bundleOperandHasAttr(i - 1, Kind);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000796}
797
Amaury Sechet392638d2016-06-14 20:27:35 +0000798void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000799 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000800 PAL = PAL.addAttribute(getContext(), i, Kind);
Devang Patel4c758ea2008-09-25 21:00:45 +0000801 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000802}
803
Amaury Secheta65a2372016-06-15 05:14:29 +0000804void InvokeInst::addAttribute(unsigned i, Attribute Attr) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000805 AttributeList PAL = getAttributes();
Amaury Secheta65a2372016-06-15 05:14:29 +0000806 PAL = PAL.addAttribute(getContext(), i, Attr);
807 setAttributes(PAL);
808}
809
Reid Klecknera0b45f42017-05-03 18:17:31 +0000810void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
811 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
812}
813
Amaury Sechet392638d2016-06-14 20:27:35 +0000814void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000815 AttributeList PAL = getAttributes();
Amaury Sechet392638d2016-06-14 20:27:35 +0000816 PAL = PAL.removeAttribute(getContext(), i, Kind);
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000817 setAttributes(PAL);
818}
819
Amaury Sechet6100adf2016-06-15 17:50:39 +0000820void InvokeInst::removeAttribute(unsigned i, StringRef Kind) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000821 AttributeList PAL = getAttributes();
Amaury Sechet6100adf2016-06-15 17:50:39 +0000822 PAL = PAL.removeAttribute(getContext(), i, Kind);
823 setAttributes(PAL);
824}
825
Reid Klecknera0b45f42017-05-03 18:17:31 +0000826void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
827 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
828}
829
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000830void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000831 AttributeList PAL = getAttributes();
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000832 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
833 setAttributes(PAL);
834}
835
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000836void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000837 AttributeList PAL = getAttributes();
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000838 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
839 setAttributes(PAL);
840}
841
Bill Wendlingfae14752011-08-12 20:24:12 +0000842LandingPadInst *InvokeInst::getLandingPadInst() const {
843 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
844}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000845
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000846//===----------------------------------------------------------------------===//
847// ReturnInst Implementation
848//===----------------------------------------------------------------------===//
849
Chris Lattner2195fc42007-02-24 00:55:48 +0000850ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000851 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000852 OperandTraits<ReturnInst>::op_end(this) -
853 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000854 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000855 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000856 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000857 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000858}
859
Owen Anderson55f1c092009-08-13 21:58:54 +0000860ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
861 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000862 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
863 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000864 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000865 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000866}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000867
Owen Anderson55f1c092009-08-13 21:58:54 +0000868ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
869 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000870 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
871 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000872 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000873 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000874}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000875
Owen Anderson55f1c092009-08-13 21:58:54 +0000876ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
877 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000878 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000879}
880
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000881unsigned ReturnInst::getNumSuccessorsV() const {
882 return getNumSuccessors();
883}
884
Devang Patelae682fb2008-02-26 17:56:20 +0000885/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
886/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000888 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889}
890
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000891BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000892 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000893}
894
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000895//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000896// ResumeInst Implementation
897//===----------------------------------------------------------------------===//
898
899ResumeInst::ResumeInst(const ResumeInst &RI)
900 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
901 OperandTraits<ResumeInst>::op_begin(this), 1) {
902 Op<0>() = RI.Op<0>();
903}
904
905ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
906 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
907 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
908 Op<0>() = Exn;
909}
910
911ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
912 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
913 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
914 Op<0>() = Exn;
915}
916
917unsigned ResumeInst::getNumSuccessorsV() const {
918 return getNumSuccessors();
919}
920
921void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
922 llvm_unreachable("ResumeInst has no successors!");
923}
924
925BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
926 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000927}
928
929//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000930// CleanupReturnInst Implementation
931//===----------------------------------------------------------------------===//
932
933CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
934 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
935 OperandTraits<CleanupReturnInst>::op_end(this) -
936 CRI.getNumOperands(),
937 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000938 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000939 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000940 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000941 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000942}
943
David Majnemer8a1c45d2015-12-12 05:38:55 +0000944void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000945 if (UnwindBB)
946 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000947
David Majnemer8a1c45d2015-12-12 05:38:55 +0000948 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000949 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000950 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000951}
952
David Majnemer8a1c45d2015-12-12 05:38:55 +0000953CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
954 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000955 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
956 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000957 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
958 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000959 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000960}
961
David Majnemer8a1c45d2015-12-12 05:38:55 +0000962CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
963 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000964 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
965 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000966 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
967 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000968 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000969}
970
971BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
972 assert(Idx == 0);
973 return getUnwindDest();
974}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000975
David Majnemer654e1302015-07-31 17:58:14 +0000976unsigned CleanupReturnInst::getNumSuccessorsV() const {
977 return getNumSuccessors();
978}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000979
David Majnemer654e1302015-07-31 17:58:14 +0000980void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
981 assert(Idx == 0);
982 setUnwindDest(B);
983}
984
985//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000986// CatchReturnInst Implementation
987//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000988void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000989 Op<0>() = CatchPad;
990 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000991}
David Majnemer654e1302015-07-31 17:58:14 +0000992
993CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
994 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000995 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
996 Op<0>() = CRI.Op<0>();
997 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000998}
999
David Majnemer8a1c45d2015-12-12 05:38:55 +00001000CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +00001001 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +00001002 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001003 OperandTraits<CatchReturnInst>::op_begin(this), 2,
1004 InsertBefore) {
1005 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +00001006}
1007
David Majnemer8a1c45d2015-12-12 05:38:55 +00001008CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +00001009 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +00001010 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00001011 OperandTraits<CatchReturnInst>::op_begin(this), 2,
1012 InsertAtEnd) {
1013 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +00001014}
1015
1016BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +00001017 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +00001018 return getSuccessor();
1019}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001020
David Majnemer654e1302015-07-31 17:58:14 +00001021unsigned CatchReturnInst::getNumSuccessorsV() const {
1022 return getNumSuccessors();
1023}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001024
David Majnemer654e1302015-07-31 17:58:14 +00001025void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +00001026 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +00001027 setSuccessor(B);
1028}
1029
1030//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +00001031// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +00001032//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +00001033
1034CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1035 unsigned NumReservedValues,
1036 const Twine &NameStr,
1037 Instruction *InsertBefore)
1038 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
1039 InsertBefore) {
1040 if (UnwindDest)
1041 ++NumReservedValues;
1042 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +00001043 setName(NameStr);
1044}
1045
David Majnemer8a1c45d2015-12-12 05:38:55 +00001046CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
1047 unsigned NumReservedValues,
1048 const Twine &NameStr, BasicBlock *InsertAtEnd)
1049 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +00001050 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00001051 if (UnwindDest)
1052 ++NumReservedValues;
1053 init(ParentPad, UnwindDest, NumReservedValues + 1);
1054 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001055}
1056
David Majnemer8a1c45d2015-12-12 05:38:55 +00001057CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
1058 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
1059 CSI.getNumOperands()) {
1060 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
1061 setNumHungOffUseOperands(ReservedSpace);
1062 Use *OL = getOperandList();
1063 const Use *InOL = CSI.getOperandList();
1064 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
1065 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +00001066}
David Majnemer8a1c45d2015-12-12 05:38:55 +00001067
1068void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
1069 unsigned NumReservedValues) {
1070 assert(ParentPad && NumReservedValues);
1071
1072 ReservedSpace = NumReservedValues;
1073 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
1074 allocHungoffUses(ReservedSpace);
1075
1076 Op<0>() = ParentPad;
1077 if (UnwindDest) {
1078 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
1079 setUnwindDest(UnwindDest);
1080 }
1081}
1082
1083/// growOperands - grow operands - This grows the operand list in response to a
1084/// push_back style of operation. This grows the number of ops by 2 times.
1085void CatchSwitchInst::growOperands(unsigned Size) {
1086 unsigned NumOperands = getNumOperands();
1087 assert(NumOperands >= 1);
1088 if (ReservedSpace >= NumOperands + Size)
1089 return;
1090 ReservedSpace = (NumOperands + Size / 2) * 2;
1091 growHungoffUses(ReservedSpace);
1092}
1093
1094void CatchSwitchInst::addHandler(BasicBlock *Handler) {
1095 unsigned OpNo = getNumOperands();
1096 growOperands(1);
1097 assert(OpNo < ReservedSpace && "Growing didn't work!");
1098 setNumHungOffUseOperands(getNumOperands() + 1);
1099 getOperandList()[OpNo] = Handler;
1100}
1101
Joseph Tremoulet0d808882016-01-05 02:37:41 +00001102void CatchSwitchInst::removeHandler(handler_iterator HI) {
1103 // Move all subsequent handlers up one.
1104 Use *EndDst = op_end() - 1;
1105 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
1106 *CurDst = *(CurDst + 1);
1107 // Null out the last handler use.
1108 *EndDst = nullptr;
1109
1110 setNumHungOffUseOperands(getNumOperands() - 1);
1111}
1112
David Majnemer8a1c45d2015-12-12 05:38:55 +00001113BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
1114 return getSuccessor(idx);
1115}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001116
David Majnemer8a1c45d2015-12-12 05:38:55 +00001117unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +00001118 return getNumSuccessors();
1119}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001120
David Majnemer8a1c45d2015-12-12 05:38:55 +00001121void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1122 setSuccessor(idx, B);
1123}
1124
1125//===----------------------------------------------------------------------===//
1126// FuncletPadInst Implementation
1127//===----------------------------------------------------------------------===//
1128void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
1129 const Twine &NameStr) {
1130 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
1131 std::copy(Args.begin(), Args.end(), op_begin());
1132 setParentPad(ParentPad);
1133 setName(NameStr);
1134}
1135
1136FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
1137 : Instruction(FPI.getType(), FPI.getOpcode(),
1138 OperandTraits<FuncletPadInst>::op_end(this) -
1139 FPI.getNumOperands(),
1140 FPI.getNumOperands()) {
1141 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
1142 setParentPad(FPI.getParentPad());
1143}
1144
1145FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1146 ArrayRef<Value *> Args, unsigned Values,
1147 const Twine &NameStr, Instruction *InsertBefore)
1148 : Instruction(ParentPad->getType(), Op,
1149 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1150 InsertBefore) {
1151 init(ParentPad, Args, NameStr);
1152}
1153
1154FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1155 ArrayRef<Value *> Args, unsigned Values,
1156 const Twine &NameStr, BasicBlock *InsertAtEnd)
1157 : Instruction(ParentPad->getType(), Op,
1158 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1159 InsertAtEnd) {
1160 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001161}
1162
1163//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001164// UnreachableInst Implementation
1165//===----------------------------------------------------------------------===//
1166
Owen Anderson55f1c092009-08-13 21:58:54 +00001167UnreachableInst::UnreachableInst(LLVMContext &Context,
1168 Instruction *InsertBefore)
1169 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001170 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001171}
Owen Anderson55f1c092009-08-13 21:58:54 +00001172UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1173 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001174 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001175}
1176
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001177unsigned UnreachableInst::getNumSuccessorsV() const {
1178 return getNumSuccessors();
1179}
1180
1181void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001182 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001183}
1184
1185BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001186 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001187}
1188
1189//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001190// BranchInst Implementation
1191//===----------------------------------------------------------------------===//
1192
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001193void BranchInst::AssertOK() {
1194 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001195 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001196 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001197}
1198
Chris Lattner2195fc42007-02-24 00:55:48 +00001199BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001200 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001201 OperandTraits<BranchInst>::op_end(this) - 1,
1202 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001203 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001204 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001205}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001206
Chris Lattner2195fc42007-02-24 00:55:48 +00001207BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1208 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001209 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001210 OperandTraits<BranchInst>::op_end(this) - 3,
1211 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001212 Op<-1>() = IfTrue;
1213 Op<-2>() = IfFalse;
1214 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001215#ifndef NDEBUG
1216 AssertOK();
1217#endif
1218}
1219
1220BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001221 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001222 OperandTraits<BranchInst>::op_end(this) - 1,
1223 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001224 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001225 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001226}
1227
1228BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1229 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001230 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001231 OperandTraits<BranchInst>::op_end(this) - 3,
1232 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001233 Op<-1>() = IfTrue;
1234 Op<-2>() = IfFalse;
1235 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001236#ifndef NDEBUG
1237 AssertOK();
1238#endif
1239}
1240
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001241BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001242 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001243 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1244 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001245 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001246 if (BI.getNumOperands() != 1) {
1247 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001248 Op<-3>() = BI.Op<-3>();
1249 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001250 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001251 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001252}
1253
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001254void BranchInst::swapSuccessors() {
1255 assert(isConditional() &&
1256 "Cannot swap successors of an unconditional branch");
1257 Op<-1>().swap(Op<-2>());
1258
1259 // Update profile metadata if present and it matches our structural
1260 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +00001261 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001262}
1263
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001264BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1265 return getSuccessor(idx);
1266}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001267
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001268unsigned BranchInst::getNumSuccessorsV() const {
1269 return getNumSuccessors();
1270}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001271
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001272void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1273 setSuccessor(idx, B);
1274}
1275
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001276//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001277// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001278//===----------------------------------------------------------------------===//
1279
Owen Andersonb6b25302009-07-14 23:09:55 +00001280static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001281 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001282 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001283 else {
1284 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001285 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001286 assert(Amt->getType()->isIntegerTy() &&
1287 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001288 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001289 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001290}
1291
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001292AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001293 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001294 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001295
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001296AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001297 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001298 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001299
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001300AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001301 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001302 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1303
1304AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1305 const Twine &Name, BasicBlock *InsertAtEnd)
1306 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1307
1308AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1309 unsigned Align, const Twine &Name,
1310 Instruction *InsertBefore)
1311 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1312 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1313 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001314 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001315 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001316 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001317}
1318
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001319AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1320 unsigned Align, const Twine &Name,
1321 BasicBlock *InsertAtEnd)
1322 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1323 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001324 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001325 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001326 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001327 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001328}
1329
Victor Hernandez8acf2952009-10-23 21:09:37 +00001330void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001331 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001332 assert(Align <= MaximumAlignment &&
1333 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001334 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1335 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001336 assert(getAlignment() == Align && "Alignment representation error!");
1337}
1338
Victor Hernandez8acf2952009-10-23 21:09:37 +00001339bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001340 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001341 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001342 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001343}
1344
Chris Lattner8b291e62008-11-26 02:54:17 +00001345/// isStaticAlloca - Return true if this alloca is in the entry block of the
1346/// function and is a constant size. If so, the code generator will fold it
1347/// into the prolog/epilog code, so it is basically free.
1348bool AllocaInst::isStaticAlloca() const {
1349 // Must be constant size.
1350 if (!isa<ConstantInt>(getArraySize())) return false;
1351
1352 // Must be in the entry block.
1353 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001354 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001355}
1356
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001357//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001358// LoadInst Implementation
1359//===----------------------------------------------------------------------===//
1360
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001361void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001362 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001363 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001364 assert(!(isAtomic() && getAlignment() == 0) &&
1365 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001366}
1367
Daniel Dunbar4975db62009-07-25 04:41:11 +00001368LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001369 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001370
Daniel Dunbar4975db62009-07-25 04:41:11 +00001371LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001372 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001373
David Blaikie0c28fd72015-05-20 21:46:30 +00001374LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001375 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001376 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001377
1378LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1379 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001380 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001381
David Blaikieb7a029872015-04-17 19:56:21 +00001382LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001383 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001384 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1385 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001386
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001387LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001388 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001389 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1390 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001391
David Blaikie15d9a4c2015-04-06 20:59:48 +00001392LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001393 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001394 SynchronizationScope SynchScope, Instruction *InsertBef)
1395 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001396 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001397 setVolatile(isVolatile);
1398 setAlignment(Align);
1399 setAtomic(Order, SynchScope);
1400 AssertOK();
1401 setName(Name);
1402}
1403
1404LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1405 unsigned Align, AtomicOrdering Order,
1406 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001407 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001408 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001409 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001410 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001411 setAlignment(Align);
1412 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001413 AssertOK();
1414 setName(Name);
1415}
1416
Daniel Dunbar27096822009-08-11 18:11:15 +00001417LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1418 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1419 Load, Ptr, InsertBef) {
1420 setVolatile(false);
1421 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001422 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001423 AssertOK();
1424 if (Name && Name[0]) setName(Name);
1425}
1426
1427LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1428 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1429 Load, Ptr, InsertAE) {
1430 setVolatile(false);
1431 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001432 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001433 AssertOK();
1434 if (Name && Name[0]) setName(Name);
1435}
1436
David Blaikie0c28fd72015-05-20 21:46:30 +00001437LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001438 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001439 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1440 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001441 setVolatile(isVolatile);
1442 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001443 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001444 AssertOK();
1445 if (Name && Name[0]) setName(Name);
1446}
1447
1448LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1449 BasicBlock *InsertAE)
1450 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1451 Load, Ptr, InsertAE) {
1452 setVolatile(isVolatile);
1453 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001454 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001455 AssertOK();
1456 if (Name && Name[0]) setName(Name);
1457}
1458
Christopher Lamb84485702007-04-22 19:24:39 +00001459void LoadInst::setAlignment(unsigned Align) {
1460 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001461 assert(Align <= MaximumAlignment &&
1462 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001463 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001464 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001465 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001466}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001467
1468//===----------------------------------------------------------------------===//
1469// StoreInst Implementation
1470//===----------------------------------------------------------------------===//
1471
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001472void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001473 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001474 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001475 "Ptr must have pointer type!");
1476 assert(getOperand(0)->getType() ==
1477 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001478 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001479 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001480 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001481}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001482
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001483StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001484 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001485
1486StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001487 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001488
Misha Brukmanb1c93172005-04-21 23:48:37 +00001489StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001490 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001491 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001492
1493StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001494 BasicBlock *InsertAtEnd)
1495 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1496
1497StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1498 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001499 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1500 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001501
1502StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1503 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001504 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1505 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001506
Misha Brukmanb1c93172005-04-21 23:48:37 +00001507StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001508 unsigned Align, AtomicOrdering Order,
1509 SynchronizationScope SynchScope,
1510 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001511 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001512 OperandTraits<StoreInst>::op_begin(this),
1513 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001514 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001515 Op<0>() = val;
1516 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001517 setVolatile(isVolatile);
1518 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001519 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001520 AssertOK();
1521}
1522
1523StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001524 unsigned Align, AtomicOrdering Order,
1525 SynchronizationScope SynchScope,
1526 BasicBlock *InsertAtEnd)
1527 : Instruction(Type::getVoidTy(val->getContext()), Store,
1528 OperandTraits<StoreInst>::op_begin(this),
1529 OperandTraits<StoreInst>::operands(this),
1530 InsertAtEnd) {
1531 Op<0>() = val;
1532 Op<1>() = addr;
1533 setVolatile(isVolatile);
1534 setAlignment(Align);
1535 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001536 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001537}
1538
Christopher Lamb84485702007-04-22 19:24:39 +00001539void StoreInst::setAlignment(unsigned Align) {
1540 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001541 assert(Align <= MaximumAlignment &&
1542 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001543 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001544 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001545 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001546}
1547
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001548//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001549// AtomicCmpXchgInst Implementation
1550//===----------------------------------------------------------------------===//
1551
1552void AtomicCmpXchgInst::Init(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 Op<0>() = Ptr;
1557 Op<1>() = Cmp;
1558 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001559 setSuccessOrdering(SuccessOrdering);
1560 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001561 setSynchScope(SynchScope);
1562
1563 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1564 "All operands must be non-null!");
1565 assert(getOperand(0)->getType()->isPointerTy() &&
1566 "Ptr must have pointer type!");
1567 assert(getOperand(1)->getType() ==
1568 cast<PointerType>(getOperand(0)->getType())->getElementType()
1569 && "Ptr must be a pointer to Cmp type!");
1570 assert(getOperand(2)->getType() ==
1571 cast<PointerType>(getOperand(0)->getType())->getElementType()
1572 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001573 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001574 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001575 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001576 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001577 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1578 "AtomicCmpXchg failure argument shall be no stronger than the success "
1579 "argument");
1580 assert(FailureOrdering != AtomicOrdering::Release &&
1581 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001582 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001583}
1584
1585AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001586 AtomicOrdering SuccessOrdering,
1587 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001588 SynchronizationScope SynchScope,
1589 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001590 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001591 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001592 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1593 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001594 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001595}
1596
1597AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001598 AtomicOrdering SuccessOrdering,
1599 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001600 SynchronizationScope SynchScope,
1601 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001602 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001603 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001604 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1605 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001606 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001607}
Tim Northover420a2162014-06-13 14:24:07 +00001608
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001609//===----------------------------------------------------------------------===//
1610// AtomicRMWInst Implementation
1611//===----------------------------------------------------------------------===//
1612
1613void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1614 AtomicOrdering Ordering,
1615 SynchronizationScope SynchScope) {
1616 Op<0>() = Ptr;
1617 Op<1>() = Val;
1618 setOperation(Operation);
1619 setOrdering(Ordering);
1620 setSynchScope(SynchScope);
1621
1622 assert(getOperand(0) && getOperand(1) &&
1623 "All operands must be non-null!");
1624 assert(getOperand(0)->getType()->isPointerTy() &&
1625 "Ptr must have pointer type!");
1626 assert(getOperand(1)->getType() ==
1627 cast<PointerType>(getOperand(0)->getType())->getElementType()
1628 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001629 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001630 "AtomicRMW instructions must be atomic!");
1631}
1632
1633AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1634 AtomicOrdering Ordering,
1635 SynchronizationScope SynchScope,
1636 Instruction *InsertBefore)
1637 : Instruction(Val->getType(), AtomicRMW,
1638 OperandTraits<AtomicRMWInst>::op_begin(this),
1639 OperandTraits<AtomicRMWInst>::operands(this),
1640 InsertBefore) {
1641 Init(Operation, Ptr, Val, Ordering, SynchScope);
1642}
1643
1644AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1645 AtomicOrdering Ordering,
1646 SynchronizationScope SynchScope,
1647 BasicBlock *InsertAtEnd)
1648 : Instruction(Val->getType(), AtomicRMW,
1649 OperandTraits<AtomicRMWInst>::op_begin(this),
1650 OperandTraits<AtomicRMWInst>::operands(this),
1651 InsertAtEnd) {
1652 Init(Operation, Ptr, Val, Ordering, SynchScope);
1653}
1654
1655//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001656// FenceInst Implementation
1657//===----------------------------------------------------------------------===//
1658
1659FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1660 SynchronizationScope SynchScope,
1661 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001662 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001663 setOrdering(Ordering);
1664 setSynchScope(SynchScope);
1665}
1666
1667FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1668 SynchronizationScope SynchScope,
1669 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001670 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001671 setOrdering(Ordering);
1672 setSynchScope(SynchScope);
1673}
1674
1675//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676// GetElementPtrInst Implementation
1677//===----------------------------------------------------------------------===//
1678
Jay Foadd1b78492011-07-25 09:48:08 +00001679void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001680 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001681 assert(getNumOperands() == 1 + IdxList.size() &&
1682 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001683 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001684 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001685 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001686}
1687
Gabor Greiff6caff662008-05-10 08:32:32 +00001688GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001689 : Instruction(GEPI.getType(), GetElementPtr,
1690 OperandTraits<GetElementPtrInst>::op_end(this) -
1691 GEPI.getNumOperands(),
1692 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001693 SourceElementType(GEPI.SourceElementType),
1694 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001695 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001696 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001697}
1698
Chris Lattner6090a422009-03-09 04:46:40 +00001699/// getIndexedType - Returns the type of the element that would be accessed with
1700/// a gep instruction with the specified parameters.
1701///
1702/// The Idxs pointer should point to a continuous piece of memory containing the
1703/// indices, either as Value* or uint64_t.
1704///
1705/// A null type is returned if the indices are invalid for the specified
1706/// pointer type.
1707///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001708template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001709static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001710 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001711 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001712 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001713
Chris Lattner6090a422009-03-09 04:46:40 +00001714 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001715 // it cannot be 'stepped over'.
1716 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001717 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001718
Dan Gohman1ecaf452008-05-31 00:58:22 +00001719 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001720 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001721 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001722 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001723 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001724 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001725 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001726 }
Craig Topperc6207612014-04-09 06:08:46 +00001727 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001728}
1729
David Blaikied288fb82015-03-30 21:41:43 +00001730Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1731 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001732}
1733
David Blaikied288fb82015-03-30 21:41:43 +00001734Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001735 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001736 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001737}
1738
David Blaikied288fb82015-03-30 21:41:43 +00001739Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1740 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001741}
1742
Chris Lattner45f15572007-04-14 00:12:57 +00001743/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1744/// zeros. If so, the result pointer and the first operand have the same
1745/// value, just potentially different types.
1746bool GetElementPtrInst::hasAllZeroIndices() const {
1747 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1748 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1749 if (!CI->isZero()) return false;
1750 } else {
1751 return false;
1752 }
1753 }
1754 return true;
1755}
1756
Chris Lattner27058292007-04-27 20:35:56 +00001757/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1758/// constant integers. If so, the result pointer and the first operand have
1759/// a constant offset between them.
1760bool GetElementPtrInst::hasAllConstantIndices() const {
1761 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1762 if (!isa<ConstantInt>(getOperand(i)))
1763 return false;
1764 }
1765 return true;
1766}
1767
Dan Gohman1b849082009-09-07 23:54:19 +00001768void GetElementPtrInst::setIsInBounds(bool B) {
1769 cast<GEPOperator>(this)->setIsInBounds(B);
1770}
Chris Lattner45f15572007-04-14 00:12:57 +00001771
Nick Lewycky28a5f252009-09-27 21:33:04 +00001772bool GetElementPtrInst::isInBounds() const {
1773 return cast<GEPOperator>(this)->isInBounds();
1774}
1775
Chandler Carruth1e140532012-12-11 10:29:10 +00001776bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1777 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001778 // Delegate to the generic GEPOperator implementation.
1779 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001780}
1781
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001782//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001783// ExtractElementInst Implementation
1784//===----------------------------------------------------------------------===//
1785
1786ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001787 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001788 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001789 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001790 ExtractElement,
1791 OperandTraits<ExtractElementInst>::op_begin(this),
1792 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001793 assert(isValidOperands(Val, Index) &&
1794 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001795 Op<0>() = Val;
1796 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001797 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001798}
1799
1800ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001801 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001802 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001803 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001804 ExtractElement,
1805 OperandTraits<ExtractElementInst>::op_begin(this),
1806 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001807 assert(isValidOperands(Val, Index) &&
1808 "Invalid extractelement instruction operands!");
1809
Gabor Greif2d3024d2008-05-26 21:33:52 +00001810 Op<0>() = Val;
1811 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001812 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001813}
1814
Chris Lattner54865b32006-04-08 04:05:48 +00001815bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001816 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001817 return false;
1818 return true;
1819}
1820
Robert Bocchino23004482006-01-10 19:05:34 +00001821//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001822// InsertElementInst Implementation
1823//===----------------------------------------------------------------------===//
1824
Chris Lattner54865b32006-04-08 04:05:48 +00001825InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001826 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001827 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001828 : Instruction(Vec->getType(), InsertElement,
1829 OperandTraits<InsertElementInst>::op_begin(this),
1830 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001831 assert(isValidOperands(Vec, Elt, Index) &&
1832 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001833 Op<0>() = Vec;
1834 Op<1>() = Elt;
1835 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001836 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001837}
1838
Chris Lattner54865b32006-04-08 04:05:48 +00001839InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001840 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001841 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001842 : Instruction(Vec->getType(), InsertElement,
1843 OperandTraits<InsertElementInst>::op_begin(this),
1844 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001845 assert(isValidOperands(Vec, Elt, Index) &&
1846 "Invalid insertelement instruction operands!");
1847
Gabor Greif2d3024d2008-05-26 21:33:52 +00001848 Op<0>() = Vec;
1849 Op<1>() = Elt;
1850 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001851 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001852}
1853
Chris Lattner54865b32006-04-08 04:05:48 +00001854bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1855 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001856 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001857 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001858
Reid Spencerd84d35b2007-02-15 02:26:10 +00001859 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001860 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001861
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001862 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001863 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001864 return true;
1865}
1866
Robert Bocchinoca27f032006-01-17 20:07:22 +00001867//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001868// ShuffleVectorInst Implementation
1869//===----------------------------------------------------------------------===//
1870
1871ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001872 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001873 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001874: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001875 cast<VectorType>(Mask->getType())->getNumElements()),
1876 ShuffleVector,
1877 OperandTraits<ShuffleVectorInst>::op_begin(this),
1878 OperandTraits<ShuffleVectorInst>::operands(this),
1879 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001880 assert(isValidOperands(V1, V2, Mask) &&
1881 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001882 Op<0>() = V1;
1883 Op<1>() = V2;
1884 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001885 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001886}
1887
1888ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001889 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001890 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001891: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1892 cast<VectorType>(Mask->getType())->getNumElements()),
1893 ShuffleVector,
1894 OperandTraits<ShuffleVectorInst>::op_begin(this),
1895 OperandTraits<ShuffleVectorInst>::operands(this),
1896 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001897 assert(isValidOperands(V1, V2, Mask) &&
1898 "Invalid shuffle vector instruction operands!");
1899
Gabor Greif2d3024d2008-05-26 21:33:52 +00001900 Op<0>() = V1;
1901 Op<1>() = V2;
1902 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001903 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001904}
1905
Mon P Wang25f01062008-11-10 04:46:22 +00001906bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001907 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001908 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001909 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001910 return false;
1911
Chris Lattner1dcb6542012-01-25 23:49:49 +00001912 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001913 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001914 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001915 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001916
1917 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001918 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1919 return true;
1920
Sanjay Patel8bd52282017-04-19 16:22:19 +00001921 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001922 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001923 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001924 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001925 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001926 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001927 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001928 return false;
1929 }
1930 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001931 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001932 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001933
Sanjay Patel8bd52282017-04-19 16:22:19 +00001934 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001935 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1936 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1937 if (CDS->getElementAsInteger(i) >= V1Size*2)
1938 return false;
1939 return true;
1940 }
1941
1942 // The bitcode reader can create a place holder for a forward reference
1943 // used as the shuffle mask. When this occurs, the shuffle mask will
1944 // fall into this case and fail. To avoid this error, do this bit of
1945 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001946 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001947 if (CE->getOpcode() == Instruction::UserOp1)
1948 return true;
1949
1950 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001951}
1952
Chris Lattnercf129702012-01-26 02:51:13 +00001953int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1954 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001955 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001956 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001957 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001958 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001959 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001960 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001961}
1962
Chris Lattnercf129702012-01-26 02:51:13 +00001963void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1964 SmallVectorImpl<int> &Result) {
1965 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001966
Sanjay Patel8bd52282017-04-19 16:22:19 +00001967 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001968 for (unsigned i = 0; i != NumElts; ++i)
1969 Result.push_back(CDS->getElementAsInteger(i));
1970 return;
1971 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001972 for (unsigned i = 0; i != NumElts; ++i) {
1973 Constant *C = Mask->getAggregateElement(i);
1974 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001975 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001976 }
1977}
1978
Dan Gohman12fce772008-05-15 19:50:34 +00001979//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001980// InsertValueInst Class
1981//===----------------------------------------------------------------------===//
1982
Jay Foad57aa6362011-07-13 10:26:04 +00001983void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1984 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001985 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001986
1987 // There's no fundamental reason why we require at least one index
1988 // (other than weirdness with &*IdxBegin being invalid; see
1989 // getelementptr's init routine for example). But there's no
1990 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001991 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00001992
1993 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001994 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001995 Op<0>() = Agg;
1996 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001997
Jay Foad57aa6362011-07-13 10:26:04 +00001998 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001999 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002000}
2001
2002InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00002003 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00002004 OperandTraits<InsertValueInst>::op_begin(this), 2),
2005 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00002006 Op<0>() = IVI.getOperand(0);
2007 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002008 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002009}
2010
2011//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00002012// ExtractValueInst Class
2013//===----------------------------------------------------------------------===//
2014
Jay Foad57aa6362011-07-13 10:26:04 +00002015void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00002016 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00002017
Jay Foad57aa6362011-07-13 10:26:04 +00002018 // There's no fundamental reason why we require at least one index.
2019 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002020 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00002021
Jay Foad57aa6362011-07-13 10:26:04 +00002022 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00002023 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00002024}
2025
2026ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00002027 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00002028 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002029 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00002030}
2031
Dan Gohman12fce772008-05-15 19:50:34 +00002032// getIndexedType - Returns the type of the element that would be extracted
2033// with an extractvalue instruction with the specified parameters.
2034//
2035// A null type is returned if the indices are invalid for the specified
2036// pointer type.
2037//
Chris Lattner229907c2011-07-18 04:54:35 +00002038Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00002039 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00002040 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002041 // We can't use CompositeType::indexValid(Index) here.
2042 // indexValid() always returns true for arrays because getelementptr allows
2043 // out-of-bounds indices. Since we don't allow those for extractvalue and
2044 // insertvalue we need to check array indexing manually.
2045 // Since the only other types we can index into are struct types it's just
2046 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00002047 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002048 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002049 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00002050 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00002051 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00002052 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002053 } else {
2054 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00002055 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00002056 }
2057
2058 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00002059 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002060 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00002061}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002062
2063//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002064// BinaryOperator Class
2065//===----------------------------------------------------------------------===//
2066
Chris Lattner2195fc42007-02-24 00:55:48 +00002067BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002068 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002069 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002070 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002071 OperandTraits<BinaryOperator>::op_begin(this),
2072 OperandTraits<BinaryOperator>::operands(this),
2073 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002074 Op<0>() = S1;
2075 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002076 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002077 setName(Name);
2078}
2079
2080BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00002081 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00002082 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00002083 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00002084 OperandTraits<BinaryOperator>::op_begin(this),
2085 OperandTraits<BinaryOperator>::operands(this),
2086 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002087 Op<0>() = S1;
2088 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00002089 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00002090 setName(Name);
2091}
2092
Chris Lattner2195fc42007-02-24 00:55:48 +00002093void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002094 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00002095 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002096 assert(LHS->getType() == RHS->getType() &&
2097 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002098#ifndef NDEBUG
2099 switch (iType) {
2100 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002101 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002102 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002103 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002104 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002105 "Tried to create an integer operation on a non-integer type!");
2106 break;
2107 case FAdd: case FSub:
2108 case FMul:
2109 assert(getType() == LHS->getType() &&
2110 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002111 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00002112 "Tried to create a floating-point operation on a "
2113 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002114 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002115 case UDiv:
2116 case SDiv:
2117 assert(getType() == LHS->getType() &&
2118 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002119 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002120 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002121 "Incorrect operand type (not integer) for S/UDIV");
2122 break;
2123 case FDiv:
2124 assert(getType() == LHS->getType() &&
2125 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002126 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002127 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002128 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002129 case URem:
2130 case SRem:
2131 assert(getType() == LHS->getType() &&
2132 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002133 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002134 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002135 "Incorrect operand type (not integer) for S/UREM");
2136 break;
2137 case FRem:
2138 assert(getType() == LHS->getType() &&
2139 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002140 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002141 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002142 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002143 case Shl:
2144 case LShr:
2145 case AShr:
2146 assert(getType() == LHS->getType() &&
2147 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002148 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002149 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002150 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002151 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002152 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002153 case And: case Or:
2154 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002155 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002156 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002157 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002158 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002159 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002160 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002161 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002162 default:
2163 break;
2164 }
2165#endif
2166}
2167
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002168BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002169 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002170 Instruction *InsertBefore) {
2171 assert(S1->getType() == S2->getType() &&
2172 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002173 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002174}
2175
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002177 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002178 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002179 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002180 InsertAtEnd->getInstList().push_back(Res);
2181 return Res;
2182}
2183
Dan Gohman5476cfd2009-08-12 16:23:25 +00002184BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002185 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002186 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002187 return new BinaryOperator(Instruction::Sub,
2188 zero, Op,
2189 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002190}
2191
Dan Gohman5476cfd2009-08-12 16:23:25 +00002192BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002193 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002194 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002195 return new BinaryOperator(Instruction::Sub,
2196 zero, Op,
2197 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002198}
2199
Dan Gohman4ab44202009-12-18 02:58:50 +00002200BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2201 Instruction *InsertBefore) {
2202 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2203 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2204}
2205
2206BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2207 BasicBlock *InsertAtEnd) {
2208 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2209 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2210}
2211
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002212BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2213 Instruction *InsertBefore) {
2214 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2215 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2216}
2217
2218BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2219 BasicBlock *InsertAtEnd) {
2220 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2221 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2222}
2223
Dan Gohman5476cfd2009-08-12 16:23:25 +00002224BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002225 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002226 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002227 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002228 Op->getType(), Name, InsertBefore);
2229}
2230
Dan Gohman5476cfd2009-08-12 16:23:25 +00002231BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002232 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002233 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002234 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002235 Op->getType(), Name, InsertAtEnd);
2236}
2237
Dan Gohman5476cfd2009-08-12 16:23:25 +00002238BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002239 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002240 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002241 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002242 Op->getType(), Name, InsertBefore);
2243}
2244
Dan Gohman5476cfd2009-08-12 16:23:25 +00002245BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002246 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002247 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002248 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002249 Op->getType(), Name, InsertAtEnd);
2250}
2251
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002252// isConstantAllOnes - Helper function for several functions below
2253static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002254 if (const Constant *C = dyn_cast<Constant>(V))
2255 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002256 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002257}
2258
Owen Andersonbb2501b2009-07-13 22:18:28 +00002259bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002260 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2261 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002262 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002263 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002264 return false;
2265}
2266
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002267bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002268 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2269 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002270 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002271 if (!IgnoreZeroSign)
2272 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2273 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2274 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002275 return false;
2276}
2277
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002278bool BinaryOperator::isNot(const Value *V) {
2279 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2280 return (Bop->getOpcode() == Instruction::Xor &&
2281 (isConstantAllOnes(Bop->getOperand(1)) ||
2282 isConstantAllOnes(Bop->getOperand(0))));
2283 return false;
2284}
2285
Chris Lattner2c7d1772005-04-24 07:28:37 +00002286Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002287 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002288}
2289
Chris Lattner2c7d1772005-04-24 07:28:37 +00002290const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2291 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002292}
2293
Dan Gohmana5b96452009-06-04 22:49:04 +00002294Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002295 return cast<BinaryOperator>(BinOp)->getOperand(1);
2296}
2297
2298const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2299 return getFNegArgument(const_cast<Value*>(BinOp));
2300}
2301
Chris Lattner2c7d1772005-04-24 07:28:37 +00002302Value *BinaryOperator::getNotArgument(Value *BinOp) {
2303 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2304 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2305 Value *Op0 = BO->getOperand(0);
2306 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002307 if (isConstantAllOnes(Op0)) return Op1;
2308
2309 assert(isConstantAllOnes(Op1));
2310 return Op0;
2311}
2312
Chris Lattner2c7d1772005-04-24 07:28:37 +00002313const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2314 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002315}
2316
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002317// Exchange the two operands to this instruction. This instruction is safe to
2318// use on any binary instruction and does not modify the semantics of the
2319// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2320// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002321bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002322 if (!isCommutative())
2323 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002324 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002325 return false;
2326}
2327
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002328//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002329// FPMathOperator Class
2330//===----------------------------------------------------------------------===//
2331
Duncan Sands05f4df82012-04-16 16:28:59 +00002332float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002333 const MDNode *MD =
2334 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002335 if (!MD)
2336 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002337 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002338 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002339}
2340
Duncan Sands05f4df82012-04-16 16:28:59 +00002341//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002342// CastInst Class
2343//===----------------------------------------------------------------------===//
2344
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345// Just determine if this cast only deals with integral->integral conversion.
2346bool CastInst::isIntegerCast() const {
2347 switch (getOpcode()) {
2348 default: return false;
2349 case Instruction::ZExt:
2350 case Instruction::SExt:
2351 case Instruction::Trunc:
2352 return true;
2353 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002354 return getOperand(0)->getType()->isIntegerTy() &&
2355 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002357}
2358
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359bool CastInst::isLosslessCast() const {
2360 // Only BitCast can be lossless, exit fast if we're not BitCast
2361 if (getOpcode() != Instruction::BitCast)
2362 return false;
2363
2364 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002365 Type *SrcTy = getOperand(0)->getType();
2366 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 if (SrcTy == DstTy)
2368 return true;
2369
Reid Spencer8d9336d2006-12-31 05:26:44 +00002370 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002371 if (SrcTy->isPointerTy())
2372 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373 return false; // Other types have no identity values
2374}
2375
2376/// This function determines if the CastInst does not require any bits to be
2377/// changed in order to effect the cast. Essentially, it identifies cases where
2378/// no code gen is necessary for the cast, hence the name no-op cast. For
2379/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002380/// # bitcast i32* %x to i8*
2381/// # bitcast <2 x i32> %x to <4 x i16>
2382/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002383/// @brief Determine if the described cast is a no-op.
2384bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002385 Type *SrcTy,
2386 Type *DestTy,
2387 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002388 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002389 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 case Instruction::Trunc:
2391 case Instruction::ZExt:
2392 case Instruction::SExt:
2393 case Instruction::FPTrunc:
2394 case Instruction::FPExt:
2395 case Instruction::UIToFP:
2396 case Instruction::SIToFP:
2397 case Instruction::FPToUI:
2398 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002399 case Instruction::AddrSpaceCast:
2400 // TODO: Target informations may give a more accurate answer here.
2401 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 case Instruction::BitCast:
2403 return true; // BitCast never modifies bits.
2404 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002405 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002406 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002408 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002409 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410 }
2411}
2412
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002413/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002414bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002415 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2416}
2417
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002418bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002419 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002420 if (getOpcode() == Instruction::PtrToInt)
2421 PtrOpTy = getOperand(0)->getType();
2422 else if (getOpcode() == Instruction::IntToPtr)
2423 PtrOpTy = getType();
2424
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002425 Type *IntPtrTy =
2426 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002427
2428 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2429}
2430
2431/// This function determines if a pair of casts can be eliminated and what
2432/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433/// instructions like this:
2434/// * %F = firstOpcode SrcTy %x to MidTy
2435/// * %S = secondOpcode MidTy %F to DstTy
2436/// The function returns a resultOpcode so these two casts can be replaced with:
2437/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002438/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439unsigned CastInst::isEliminableCastPair(
2440 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002441 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2442 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443 // Define the 144 possibilities for these two cast instructions. The values
2444 // in this matrix determine what to do in a given situation and select the
2445 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002446 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 // the following cast properties:
2448 //
2449 // Size Compare Source Destination
2450 // Operator Src ? Size Type Sign Type Sign
2451 // -------- ------------ ------------------- ---------------------
2452 // TRUNC > Integer Any Integral Any
2453 // ZEXT < Integral Unsigned Integer Any
2454 // SEXT < Integral Signed Integer Any
2455 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002456 // FPTOSI n/a FloatPt n/a Integral Signed
2457 // UITOFP n/a Integral Unsigned FloatPt n/a
2458 // SITOFP n/a Integral Signed FloatPt n/a
2459 // FPTRUNC > FloatPt n/a FloatPt n/a
2460 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461 // PTRTOINT n/a Pointer n/a Integral Unsigned
2462 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002463 // BITCAST = FirstClass n/a FirstClass n/a
2464 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002465 //
2466 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002467 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2468 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002469 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002470 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002471 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002472 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002473 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2475 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002476 // T F F U S F F P I B A -+
2477 // R Z S P P I I T P 2 N T S |
2478 // U E E 2 2 2 2 R E I T C C +- secondOp
2479 // N X X U S F F N X N 2 V V |
2480 // C T T I I P P C T T P T T -+
2481 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002482 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002483 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2484 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2485 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2486 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2487 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002488 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002489 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2490 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2491 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2492 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2493 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002495
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002496 // TODO: This logic could be encoded into the table above and handled in the
2497 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002498 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002499 // merging. However, any pair of bitcasts are allowed.
2500 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2501 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2502 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002503
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002504 // Check if any of the casts convert scalars <-> vectors.
2505 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2506 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2507 if (!AreBothBitcasts)
2508 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509
2510 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2511 [secondOp-Instruction::CastOpsBegin];
2512 switch (ElimCase) {
2513 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002514 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515 return 0;
2516 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002517 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 return firstOp;
2519 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002520 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521 return secondOp;
2522 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002523 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002524 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002525 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002526 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527 return firstOp;
2528 return 0;
2529 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002530 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002531 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002532 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533 return firstOp;
2534 return 0;
2535 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002536 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002537 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002538 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539 return secondOp;
2540 return 0;
2541 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002542 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002543 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002544 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545 return secondOp;
2546 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002547 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002548 // Cannot simplify if address spaces are different!
2549 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2550 return 0;
2551
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002552 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002553 // We can still fold this without knowing the actual sizes as long we
2554 // know that the intermediate pointer is the largest possible
2555 // pointer size.
2556 // FIXME: Is this always true?
2557 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002558 return Instruction::BitCast;
2559
2560 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002561 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002562 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002563 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564 if (MidSize >= PtrSize)
2565 return Instruction::BitCast;
2566 return 0;
2567 }
2568 case 8: {
2569 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2570 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2571 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002572 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2573 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574 if (SrcSize == DstSize)
2575 return Instruction::BitCast;
2576 else if (SrcSize < DstSize)
2577 return firstOp;
2578 return secondOp;
2579 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002580 case 9:
2581 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582 return Instruction::ZExt;
2583 case 10:
2584 // fpext followed by ftrunc is allowed if the bit size returned to is
2585 // the same as the original, in which case its just a bitcast
2586 if (SrcTy == DstTy)
2587 return Instruction::BitCast;
2588 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002589 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002591 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002592 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002593 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002594 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2595 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596 if (SrcSize <= PtrSize && SrcSize == DstSize)
2597 return Instruction::BitCast;
2598 return 0;
2599 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002600 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002601 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2602 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2603 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2604 return Instruction::AddrSpaceCast;
2605 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002606 case 13:
2607 // FIXME: this state can be merged with (1), but the following assert
2608 // is useful to check the correcteness of the sequence due to semantic
2609 // change of bitcast.
2610 assert(
2611 SrcTy->isPtrOrPtrVectorTy() &&
2612 MidTy->isPtrOrPtrVectorTy() &&
2613 DstTy->isPtrOrPtrVectorTy() &&
2614 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2615 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2616 "Illegal addrspacecast, bitcast sequence!");
2617 // Allowed, use first cast's opcode
2618 return firstOp;
2619 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002620 // bitcast, addrspacecast -> addrspacecast if the element type of
2621 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002622 if (SrcTy->getScalarType()->getPointerElementType() ==
2623 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002624 return Instruction::AddrSpaceCast;
2625 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002626 case 15:
2627 // FIXME: this state can be merged with (1), but the following assert
2628 // is useful to check the correcteness of the sequence due to semantic
2629 // change of bitcast.
2630 assert(
2631 SrcTy->isIntOrIntVectorTy() &&
2632 MidTy->isPtrOrPtrVectorTy() &&
2633 DstTy->isPtrOrPtrVectorTy() &&
2634 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2635 "Illegal inttoptr, bitcast sequence!");
2636 // Allowed, use first cast's opcode
2637 return firstOp;
2638 case 16:
2639 // FIXME: this state can be merged with (2), but the following assert
2640 // is useful to check the correcteness of the sequence due to semantic
2641 // change of bitcast.
2642 assert(
2643 SrcTy->isPtrOrPtrVectorTy() &&
2644 MidTy->isPtrOrPtrVectorTy() &&
2645 DstTy->isIntOrIntVectorTy() &&
2646 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2647 "Illegal bitcast, ptrtoint sequence!");
2648 // Allowed, use second cast's opcode
2649 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002650 case 17:
2651 // (sitofp (zext x)) -> (uitofp x)
2652 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002654 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002656 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657 default:
Craig Topperc514b542012-02-05 22:14:15 +00002658 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660}
2661
Chris Lattner229907c2011-07-18 04:54:35 +00002662CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002663 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002664 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665 // Construct and return the appropriate CastInst subclass
2666 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002667 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2668 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2669 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2670 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2671 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2672 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2673 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2674 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2675 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2676 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2677 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2678 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2679 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2680 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682}
2683
Chris Lattner229907c2011-07-18 04:54:35 +00002684CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002685 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002686 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687 // Construct and return the appropriate CastInst subclass
2688 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002689 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2690 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2691 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2692 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2693 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2694 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2695 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2696 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2697 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2698 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2699 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2700 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2701 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2702 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002703 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002704}
2705
Chris Lattner229907c2011-07-18 04:54:35 +00002706CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002707 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002708 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002709 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002710 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2711 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002712}
2713
Chris Lattner229907c2011-07-18 04:54:35 +00002714CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002715 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002716 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002717 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002718 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2719 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002720}
2721
Chris Lattner229907c2011-07-18 04:54:35 +00002722CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002723 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002724 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002725 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002726 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2727 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002728}
2729
Chris Lattner229907c2011-07-18 04:54:35 +00002730CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002731 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002732 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002733 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002734 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2735 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002736}
2737
Chris Lattner229907c2011-07-18 04:54:35 +00002738CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002739 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002740 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002741 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002742 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2743 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002744}
2745
Chris Lattner229907c2011-07-18 04:54:35 +00002746CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002747 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002748 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002749 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002750 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2751 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002752}
2753
Chris Lattner229907c2011-07-18 04:54:35 +00002754CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002755 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002756 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002757 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2758 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2759 "Invalid cast");
2760 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002761 assert((!Ty->isVectorTy() ||
2762 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002763 "Invalid cast");
2764
Matt Arsenault065ced92013-07-31 00:17:33 +00002765 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002766 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002767
Matt Arsenault740980e2014-07-14 17:24:35 +00002768 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002769}
2770
2771/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002772CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2773 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002774 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002775 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2776 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002777 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002778 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002779 assert((!Ty->isVectorTy() ||
2780 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002781 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002782
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002783 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002784 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002785
Matt Arsenault740980e2014-07-14 17:24:35 +00002786 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2787}
2788
2789CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2790 Value *S, Type *Ty,
2791 const Twine &Name,
2792 BasicBlock *InsertAtEnd) {
2793 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2794 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2795
2796 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2797 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2798
2799 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2800}
2801
2802CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2803 Value *S, Type *Ty,
2804 const Twine &Name,
2805 Instruction *InsertBefore) {
2806 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2807 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2808
2809 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002810 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2811
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002812 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002813}
2814
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002815CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2816 const Twine &Name,
2817 Instruction *InsertBefore) {
2818 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2819 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2820 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2821 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2822
2823 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2824}
2825
Matt Arsenault740980e2014-07-14 17:24:35 +00002826CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002827 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002828 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002829 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002830 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002831 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2832 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002833 Instruction::CastOps opcode =
2834 (SrcBits == DstBits ? Instruction::BitCast :
2835 (SrcBits > DstBits ? Instruction::Trunc :
2836 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002837 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002838}
2839
Chris Lattner229907c2011-07-18 04:54:35 +00002840CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002841 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002842 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002843 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002844 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002845 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2846 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002847 Instruction::CastOps opcode =
2848 (SrcBits == DstBits ? Instruction::BitCast :
2849 (SrcBits > DstBits ? Instruction::Trunc :
2850 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002851 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002852}
2853
Chris Lattner229907c2011-07-18 04:54:35 +00002854CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002855 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002856 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002857 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002858 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002859 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2860 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002861 Instruction::CastOps opcode =
2862 (SrcBits == DstBits ? Instruction::BitCast :
2863 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002864 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002865}
2866
Chris Lattner229907c2011-07-18 04:54:35 +00002867CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002868 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002869 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002870 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002871 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002872 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2873 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002874 Instruction::CastOps opcode =
2875 (SrcBits == DstBits ? Instruction::BitCast :
2876 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002877 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002878}
2879
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002880// Check whether it is valid to call getCastOpcode for these types.
2881// This routine must be kept in sync with getCastOpcode.
2882bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2883 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2884 return false;
2885
2886 if (SrcTy == DestTy)
2887 return true;
2888
2889 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2890 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2891 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2892 // An element by element cast. Valid if casting the elements is valid.
2893 SrcTy = SrcVecTy->getElementType();
2894 DestTy = DestVecTy->getElementType();
2895 }
2896
2897 // Get the bit sizes, we'll need these
2898 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2899 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2900
2901 // Run through the possibilities ...
2902 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002903 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002904 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002905 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002906 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002907 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002908 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002909 // Casting from something else
2910 return SrcTy->isPointerTy();
2911 }
2912 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2913 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002914 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002915 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002916 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002917 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002918 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002919 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002920 return false;
2921 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002922 if (DestTy->isVectorTy()) // Casting to vector
2923 return DestBits == SrcBits;
2924 if (DestTy->isPointerTy()) { // Casting to pointer
2925 if (SrcTy->isPointerTy()) // Casting from pointer
2926 return true;
2927 return SrcTy->isIntegerTy(); // Casting from integral
2928 }
2929 if (DestTy->isX86_MMXTy()) {
2930 if (SrcTy->isVectorTy())
2931 return DestBits == SrcBits; // 64-bit vector to MMX
2932 return false;
2933 } // Casting to something else
2934 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002935}
2936
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002937bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2938 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2939 return false;
2940
2941 if (SrcTy == DestTy)
2942 return true;
2943
2944 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2945 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2946 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2947 // An element by element cast. Valid if casting the elements is valid.
2948 SrcTy = SrcVecTy->getElementType();
2949 DestTy = DestVecTy->getElementType();
2950 }
2951 }
2952 }
2953
2954 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2955 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2956 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2957 }
2958 }
2959
2960 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2961 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2962
2963 // Could still have vectors of pointers if the number of elements doesn't
2964 // match
2965 if (SrcBits == 0 || DestBits == 0)
2966 return false;
2967
2968 if (SrcBits != DestBits)
2969 return false;
2970
2971 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2972 return false;
2973
2974 return true;
2975}
2976
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002977bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002978 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002979 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2980 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002981 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002982 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2983 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002984 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002985
2986 return isBitCastable(SrcTy, DestTy);
2987}
2988
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002989// Provide a way to get a "cast" where the cast opcode is inferred from the
2990// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002991// logic in the castIsValid function below. This axiom should hold:
2992// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2993// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002994// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002995// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002996Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002997CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002998 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2999 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003000
Duncan Sands55e50902008-01-06 10:12:28 +00003001 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
3002 "Only first class types are castable!");
3003
Duncan Sandsa8514532011-05-18 07:13:41 +00003004 if (SrcTy == DestTy)
3005 return BitCast;
3006
Matt Arsenaultcacbb232013-07-30 20:45:05 +00003007 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00003008 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
3009 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00003010 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
3011 // An element by element cast. Find the appropriate opcode based on the
3012 // element types.
3013 SrcTy = SrcVecTy->getElementType();
3014 DestTy = DestVecTy->getElementType();
3015 }
3016
3017 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00003018 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
3019 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00003020
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003021 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00003022 if (DestTy->isIntegerTy()) { // Casting to integral
3023 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003024 if (DestBits < SrcBits)
3025 return Trunc; // int -> smaller int
3026 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00003027 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003028 return SExt; // signed -> SEXT
3029 else
3030 return ZExt; // unsigned -> ZEXT
3031 } else {
3032 return BitCast; // Same size, No-op cast
3033 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003034 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00003035 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003036 return FPToSI; // FP -> sint
3037 else
3038 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00003039 } else if (SrcTy->isVectorTy()) {
3040 assert(DestBits == SrcBits &&
3041 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003042 return BitCast; // Same size, no-op cast
3043 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00003044 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045 "Casting from a value that is not first-class type");
3046 return PtrToInt; // ptr -> int
3047 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00003048 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
3049 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00003050 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003051 return SIToFP; // sint -> FP
3052 else
3053 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00003054 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003055 if (DestBits < SrcBits) {
3056 return FPTrunc; // FP -> smaller FP
3057 } else if (DestBits > SrcBits) {
3058 return FPExt; // FP -> larger FP
3059 } else {
3060 return BitCast; // same size, no-op cast
3061 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00003062 } else if (SrcTy->isVectorTy()) {
3063 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00003064 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00003065 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003066 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003067 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00003068 } else if (DestTy->isVectorTy()) {
3069 assert(DestBits == SrcBits &&
3070 "Illegal cast to vector (wrong type or size)");
3071 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00003072 } else if (DestTy->isPointerTy()) {
3073 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003074 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
3075 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003076 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00003077 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003078 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003079 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003080 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00003081 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00003082 if (SrcTy->isVectorTy()) {
3083 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00003084 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00003085 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003086 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003087 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00003088 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003089}
3090
3091//===----------------------------------------------------------------------===//
3092// CastInst SubClass Constructors
3093//===----------------------------------------------------------------------===//
3094
3095/// Check that the construction parameters for a CastInst are correct. This
3096/// could be broken out into the separate constructors but it is useful to have
3097/// it in one place and to eliminate the redundant code for getting the sizes
3098/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003099bool
Chris Lattner229907c2011-07-18 04:54:35 +00003100CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003101 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003102 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003103
Chris Lattner37bc78a2010-01-26 21:51:43 +00003104 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3105 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003106 return false;
3107
3108 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003109 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3110 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003111
Duncan Sands7f646562011-05-18 09:21:57 +00003112 // If these are vector types, get the lengths of the vectors (using zero for
3113 // scalar types means that checking that vector lengths match also checks that
3114 // scalars are not being converted to vectors or vectors to scalars).
3115 unsigned SrcLength = SrcTy->isVectorTy() ?
3116 cast<VectorType>(SrcTy)->getNumElements() : 0;
3117 unsigned DstLength = DstTy->isVectorTy() ?
3118 cast<VectorType>(DstTy)->getNumElements() : 0;
3119
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003120 // Switch on the opcode provided
3121 switch (op) {
3122 default: return false; // This is an input error
3123 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003124 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3125 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003126 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003127 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3128 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003129 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003130 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3131 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003132 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003133 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3134 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003135 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003136 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3137 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003138 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003139 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003140 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3141 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003142 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003143 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003144 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3145 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003146 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003147 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003148 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003149 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3150 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3151 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003152 return SrcTy->getScalarType()->isPointerTy() &&
3153 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003154 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003155 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003156 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003157 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3158 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3159 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003160 return SrcTy->getScalarType()->isIntegerTy() &&
3161 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003162 case Instruction::BitCast: {
3163 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3164 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3165
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166 // BitCast implies a no-op cast of type only. No bits change.
3167 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003168 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003169 return false;
3170
Alp Tokerf907b892013-12-05 05:44:44 +00003171 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003172 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003173 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003174 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3175
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003176 // If both are pointers then the address spaces must match.
3177 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3178 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003179
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003180 // A vector of pointers must have the same number of elements.
3181 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3182 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3183 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3184
3185 return false;
3186 }
3187
3188 return true;
3189 }
3190 case Instruction::AddrSpaceCast: {
3191 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3192 if (!SrcPtrTy)
3193 return false;
3194
3195 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3196 if (!DstPtrTy)
3197 return false;
3198
3199 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3200 return false;
3201
3202 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3203 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3204 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3205
3206 return false;
3207 }
3208
3209 return true;
3210 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003211 }
3212}
3213
3214TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003215 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003217 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003218}
3219
3220TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003221 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003223 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003224}
3225
3226ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003227 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003229 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003230}
3231
3232ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003233 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003235 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003236}
3237SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003238 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003239) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241}
3242
Jeff Cohencc08c832006-12-02 02:22:01 +00003243SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003244 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003245) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003247}
3248
3249FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003250 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003251) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003253}
3254
3255FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003256 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003257) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003258 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003259}
3260
3261FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003262 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003263) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003264 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003265}
3266
3267FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003268 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003269) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003270 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003271}
3272
3273UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003274 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003275) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003276 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003277}
3278
3279UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003280 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003281) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003282 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003283}
3284
3285SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003286 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003287) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003288 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003289}
3290
3291SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003292 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003293) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003294 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003295}
3296
3297FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003298 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003299) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003300 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003301}
3302
3303FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003304 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003305) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003306 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003307}
3308
3309FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003310 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003311) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003312 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003313}
3314
3315FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003316 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003317) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003318 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003319}
3320
3321PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003322 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003323) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003324 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003325}
3326
3327PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003328 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003329) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003330 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003331}
3332
3333IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003334 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003335) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003336 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003337}
3338
3339IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003340 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003341) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003342 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003343}
3344
3345BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003346 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003347) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003348 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003349}
3350
3351BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003352 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003353) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003354 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003355}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003356
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003357AddrSpaceCastInst::AddrSpaceCastInst(
3358 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3359) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3360 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3361}
3362
3363AddrSpaceCastInst::AddrSpaceCastInst(
3364 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3365) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3366 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3367}
3368
Chris Lattnerf16dc002006-09-17 19:29:56 +00003369//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003370// CmpInst Classes
3371//===----------------------------------------------------------------------===//
3372
Craig Topper1c3f2832015-12-15 06:11:33 +00003373CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3374 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003375 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003376 OperandTraits<CmpInst>::op_begin(this),
3377 OperandTraits<CmpInst>::operands(this),
3378 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003379 Op<0>() = LHS;
3380 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003381 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003382 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003383}
Gabor Greiff6caff662008-05-10 08:32:32 +00003384
Craig Topper1c3f2832015-12-15 06:11:33 +00003385CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3386 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003387 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003388 OperandTraits<CmpInst>::op_begin(this),
3389 OperandTraits<CmpInst>::operands(this),
3390 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003391 Op<0>() = LHS;
3392 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003393 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003394 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003395}
3396
3397CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003398CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003399 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003400 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003401 if (InsertBefore)
3402 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3403 S1, S2, Name);
3404 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003405 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003406 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003407 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003408
3409 if (InsertBefore)
3410 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3411 S1, S2, Name);
3412 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003413 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003414 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003415}
3416
3417CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003418CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003419 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003420 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003421 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3422 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003423 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003424 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3425 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003426}
3427
3428void CmpInst::swapOperands() {
3429 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3430 IC->swapOperands();
3431 else
3432 cast<FCmpInst>(this)->swapOperands();
3433}
3434
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003435bool CmpInst::isCommutative() const {
3436 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003437 return IC->isCommutative();
3438 return cast<FCmpInst>(this)->isCommutative();
3439}
3440
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003441bool CmpInst::isEquality() const {
3442 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003443 return IC->isEquality();
3444 return cast<FCmpInst>(this)->isEquality();
3445}
3446
Dan Gohman4e724382008-05-31 02:47:54 +00003447CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003448 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003449 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003450 case ICMP_EQ: return ICMP_NE;
3451 case ICMP_NE: return ICMP_EQ;
3452 case ICMP_UGT: return ICMP_ULE;
3453 case ICMP_ULT: return ICMP_UGE;
3454 case ICMP_UGE: return ICMP_ULT;
3455 case ICMP_ULE: return ICMP_UGT;
3456 case ICMP_SGT: return ICMP_SLE;
3457 case ICMP_SLT: return ICMP_SGE;
3458 case ICMP_SGE: return ICMP_SLT;
3459 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003460
Dan Gohman4e724382008-05-31 02:47:54 +00003461 case FCMP_OEQ: return FCMP_UNE;
3462 case FCMP_ONE: return FCMP_UEQ;
3463 case FCMP_OGT: return FCMP_ULE;
3464 case FCMP_OLT: return FCMP_UGE;
3465 case FCMP_OGE: return FCMP_ULT;
3466 case FCMP_OLE: return FCMP_UGT;
3467 case FCMP_UEQ: return FCMP_ONE;
3468 case FCMP_UNE: return FCMP_OEQ;
3469 case FCMP_UGT: return FCMP_OLE;
3470 case FCMP_ULT: return FCMP_OGE;
3471 case FCMP_UGE: return FCMP_OLT;
3472 case FCMP_ULE: return FCMP_OGT;
3473 case FCMP_ORD: return FCMP_UNO;
3474 case FCMP_UNO: return FCMP_ORD;
3475 case FCMP_TRUE: return FCMP_FALSE;
3476 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003477 }
3478}
3479
Tim Northoverde3aea0412016-08-17 20:25:25 +00003480StringRef CmpInst::getPredicateName(Predicate Pred) {
3481 switch (Pred) {
3482 default: return "unknown";
3483 case FCmpInst::FCMP_FALSE: return "false";
3484 case FCmpInst::FCMP_OEQ: return "oeq";
3485 case FCmpInst::FCMP_OGT: return "ogt";
3486 case FCmpInst::FCMP_OGE: return "oge";
3487 case FCmpInst::FCMP_OLT: return "olt";
3488 case FCmpInst::FCMP_OLE: return "ole";
3489 case FCmpInst::FCMP_ONE: return "one";
3490 case FCmpInst::FCMP_ORD: return "ord";
3491 case FCmpInst::FCMP_UNO: return "uno";
3492 case FCmpInst::FCMP_UEQ: return "ueq";
3493 case FCmpInst::FCMP_UGT: return "ugt";
3494 case FCmpInst::FCMP_UGE: return "uge";
3495 case FCmpInst::FCMP_ULT: return "ult";
3496 case FCmpInst::FCMP_ULE: return "ule";
3497 case FCmpInst::FCMP_UNE: return "une";
3498 case FCmpInst::FCMP_TRUE: return "true";
3499 case ICmpInst::ICMP_EQ: return "eq";
3500 case ICmpInst::ICMP_NE: return "ne";
3501 case ICmpInst::ICMP_SGT: return "sgt";
3502 case ICmpInst::ICMP_SGE: return "sge";
3503 case ICmpInst::ICMP_SLT: return "slt";
3504 case ICmpInst::ICMP_SLE: return "sle";
3505 case ICmpInst::ICMP_UGT: return "ugt";
3506 case ICmpInst::ICMP_UGE: return "uge";
3507 case ICmpInst::ICMP_ULT: return "ult";
3508 case ICmpInst::ICMP_ULE: return "ule";
3509 }
3510}
3511
Reid Spencer266e42b2006-12-23 06:05:41 +00003512ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3513 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003514 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003515 case ICMP_EQ: case ICMP_NE:
3516 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3517 return pred;
3518 case ICMP_UGT: return ICMP_SGT;
3519 case ICMP_ULT: return ICMP_SLT;
3520 case ICMP_UGE: return ICMP_SGE;
3521 case ICMP_ULE: return ICMP_SLE;
3522 }
3523}
3524
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003525ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3526 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003527 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003528 case ICMP_EQ: case ICMP_NE:
3529 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3530 return pred;
3531 case ICMP_SGT: return ICMP_UGT;
3532 case ICMP_SLT: return ICMP_ULT;
3533 case ICMP_SGE: return ICMP_UGE;
3534 case ICMP_SLE: return ICMP_ULE;
3535 }
3536}
3537
Dan Gohman4e724382008-05-31 02:47:54 +00003538CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003539 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003540 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003541 case ICMP_EQ: case ICMP_NE:
3542 return pred;
3543 case ICMP_SGT: return ICMP_SLT;
3544 case ICMP_SLT: return ICMP_SGT;
3545 case ICMP_SGE: return ICMP_SLE;
3546 case ICMP_SLE: return ICMP_SGE;
3547 case ICMP_UGT: return ICMP_ULT;
3548 case ICMP_ULT: return ICMP_UGT;
3549 case ICMP_UGE: return ICMP_ULE;
3550 case ICMP_ULE: return ICMP_UGE;
3551
Reid Spencerd9436b62006-11-20 01:22:35 +00003552 case FCMP_FALSE: case FCMP_TRUE:
3553 case FCMP_OEQ: case FCMP_ONE:
3554 case FCMP_UEQ: case FCMP_UNE:
3555 case FCMP_ORD: case FCMP_UNO:
3556 return pred;
3557 case FCMP_OGT: return FCMP_OLT;
3558 case FCMP_OLT: return FCMP_OGT;
3559 case FCMP_OGE: return FCMP_OLE;
3560 case FCMP_OLE: return FCMP_OGE;
3561 case FCMP_UGT: return FCMP_ULT;
3562 case FCMP_ULT: return FCMP_UGT;
3563 case FCMP_UGE: return FCMP_ULE;
3564 case FCMP_ULE: return FCMP_UGE;
3565 }
3566}
3567
Sanjoy Das6e78b172015-10-22 19:57:34 +00003568CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3569 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3570
3571 switch (pred) {
3572 default:
3573 llvm_unreachable("Unknown predicate!");
3574 case CmpInst::ICMP_ULT:
3575 return CmpInst::ICMP_SLT;
3576 case CmpInst::ICMP_ULE:
3577 return CmpInst::ICMP_SLE;
3578 case CmpInst::ICMP_UGT:
3579 return CmpInst::ICMP_SGT;
3580 case CmpInst::ICMP_UGE:
3581 return CmpInst::ICMP_SGE;
3582 }
3583}
3584
Craig Topper1c3f2832015-12-15 06:11:33 +00003585bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003586 switch (predicate) {
3587 default: return false;
3588 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3589 case ICmpInst::ICMP_UGE: return true;
3590 }
3591}
3592
Craig Topper1c3f2832015-12-15 06:11:33 +00003593bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003594 switch (predicate) {
3595 default: return false;
3596 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3597 case ICmpInst::ICMP_SGE: return true;
3598 }
3599}
3600
Craig Topper1c3f2832015-12-15 06:11:33 +00003601bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003602 switch (predicate) {
3603 default: return false;
3604 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3605 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3606 case FCmpInst::FCMP_ORD: return true;
3607 }
3608}
3609
Craig Topper1c3f2832015-12-15 06:11:33 +00003610bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003611 switch (predicate) {
3612 default: return false;
3613 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3614 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3615 case FCmpInst::FCMP_UNO: return true;
3616 }
3617}
3618
Craig Topper1c3f2832015-12-15 06:11:33 +00003619bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003620 switch(predicate) {
3621 default: return false;
3622 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3623 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3624 }
3625}
3626
Craig Topper1c3f2832015-12-15 06:11:33 +00003627bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003628 switch(predicate) {
3629 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3630 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3631 default: return false;
3632 }
3633}
3634
Chad Rosier99bc4802016-04-21 16:18:02 +00003635bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003636 // If the predicates match, then we know the first condition implies the
3637 // second is true.
3638 if (Pred1 == Pred2)
3639 return true;
3640
3641 switch (Pred1) {
3642 default:
3643 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003644 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003645 // 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 +00003646 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3647 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003648 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3649 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3650 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3651 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3652 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3653 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3654 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3655 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003656 }
3657 return false;
3658}
3659
Chad Rosier99bc4802016-04-21 16:18:02 +00003660bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003661 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003662}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003663
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003664//===----------------------------------------------------------------------===//
3665// SwitchInst Implementation
3666//===----------------------------------------------------------------------===//
3667
Chris Lattnerbaf00152010-11-17 05:41:46 +00003668void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3669 assert(Value && Default && NumReserved);
3670 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003671 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003672 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003673
Pete Coopereb31b682015-05-21 22:48:54 +00003674 Op<0>() = Value;
3675 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003676}
3677
Chris Lattner2195fc42007-02-24 00:55:48 +00003678/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3679/// switch on and a default destination. The number of additional cases can
3680/// be specified here to make memory allocation more efficient. This
3681/// constructor can also autoinsert before another instruction.
3682SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3683 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003684 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003685 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003686 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003687}
3688
3689/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3690/// switch on and a default destination. The number of additional cases can
3691/// be specified here to make memory allocation more efficient. This
3692/// constructor also autoinserts at the end of the specified BasicBlock.
3693SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3694 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003695 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003696 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003697 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003698}
3699
Misha Brukmanb1c93172005-04-21 23:48:37 +00003700SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003701 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003702 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003703 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003704 Use *OL = getOperandList();
3705 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003706 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003707 OL[i] = InOL[i];
3708 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003709 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003710 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003711}
3712
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003713
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003714/// addCase - Add an entry to the switch instruction...
3715///
Chris Lattner47ac1872005-02-24 05:32:09 +00003716void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003717 unsigned NewCaseIdx = getNumCases();
3718 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003719 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003720 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003721 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003722 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003723 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003724 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003725 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003726 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003727}
3728
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003729/// removeCase - This method removes the specified case and its successor
3730/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003731SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3732 unsigned idx = I->getCaseIndex();
3733
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003734 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003735
3736 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003737 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003738
Jay Foad14277722011-02-01 09:22:34 +00003739 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003740 if (2 + (idx + 1) * 2 != NumOps) {
3741 OL[2 + idx * 2] = OL[NumOps - 2];
3742 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003743 }
3744
3745 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003746 OL[NumOps-2].set(nullptr);
3747 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003748 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003749
3750 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003751}
3752
Jay Foade98f29d2011-04-01 08:00:58 +00003753/// growOperands - grow operands - This grows the operand list in response
3754/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003755///
Jay Foade98f29d2011-04-01 08:00:58 +00003756void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003757 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003758 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003759
3760 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003761 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003762}
3763
3764
3765BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3766 return getSuccessor(idx);
3767}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00003768
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003769unsigned SwitchInst::getNumSuccessorsV() const {
3770 return getNumSuccessors();
3771}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00003772
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003773void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3774 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003775}
Chris Lattnerf22be932004-10-15 23:52:53 +00003776
Chris Lattner3ed871f2009-10-27 19:13:16 +00003777//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003778// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003779//===----------------------------------------------------------------------===//
3780
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003781void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003782 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003783 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003784 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003785 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003786 allocHungoffUses(ReservedSpace);
3787
Pete Coopereb31b682015-05-21 22:48:54 +00003788 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003789}
3790
3791
Jay Foade98f29d2011-04-01 08:00:58 +00003792/// growOperands - grow operands - This grows the operand list in response
3793/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003794///
Jay Foade98f29d2011-04-01 08:00:58 +00003795void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003796 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003797 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003798
3799 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003800 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003801}
3802
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003803IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3804 Instruction *InsertBefore)
3805: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003806 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003807 init(Address, NumCases);
3808}
3809
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003810IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3811 BasicBlock *InsertAtEnd)
3812: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003813 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003814 init(Address, NumCases);
3815}
3816
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003817IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003818 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3819 nullptr, IBI.getNumOperands()) {
3820 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003821 Use *OL = getOperandList();
3822 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003823 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3824 OL[i] = InOL[i];
3825 SubclassOptionalData = IBI.SubclassOptionalData;
3826}
3827
Chris Lattner3ed871f2009-10-27 19:13:16 +00003828/// addDestination - Add a destination.
3829///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003830void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003831 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003832 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003833 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003834 // Initialize some new operands.
3835 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003836 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003837 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003838}
3839
3840/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003841/// indirectbr instruction.
3842void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003843 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3844
3845 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003846 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003847
3848 // Replace this value with the last one.
3849 OL[idx+1] = OL[NumOps-1];
3850
3851 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003852 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003853 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003854}
3855
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003856BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003857 return getSuccessor(idx);
3858}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00003859
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003860unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003861 return getNumSuccessors();
3862}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00003863
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003864void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003865 setSuccessor(idx, B);
3866}
3867
3868//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003869// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003870//===----------------------------------------------------------------------===//
3871
Chris Lattnerf22be932004-10-15 23:52:53 +00003872// Define these methods here so vtables don't get emitted into every translation
3873// unit that uses these classes.
3874
Pete Cooper75403d72015-06-24 20:22:23 +00003875GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003876 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003877}
3878
Pete Cooper75403d72015-06-24 20:22:23 +00003879BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003880 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003881}
3882
Pete Cooper75403d72015-06-24 20:22:23 +00003883FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003884 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003885}
3886
Pete Cooper75403d72015-06-24 20:22:23 +00003887ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003888 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003892 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003893}
3894
Pete Cooper75403d72015-06-24 20:22:23 +00003895InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003896 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003897}
3898
Pete Cooper75403d72015-06-24 20:22:23 +00003899AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003900 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003901 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003902 (Value *)getOperand(0), getAlignment());
3903 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003904 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003905 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003906}
3907
Pete Cooper75403d72015-06-24 20:22:23 +00003908LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003909 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3910 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003911}
3912
Pete Cooper75403d72015-06-24 20:22:23 +00003913StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003914 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003915 getAlignment(), getOrdering(), getSynchScope());
3916
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003917}
3918
Pete Cooper75403d72015-06-24 20:22:23 +00003919AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003920 AtomicCmpXchgInst *Result =
3921 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003922 getSuccessOrdering(), getFailureOrdering(),
3923 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003924 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003925 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003926 return Result;
3927}
3928
Pete Cooper75403d72015-06-24 20:22:23 +00003929AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003930 AtomicRMWInst *Result =
3931 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3932 getOrdering(), getSynchScope());
3933 Result->setVolatile(isVolatile());
3934 return Result;
3935}
3936
Pete Cooper75403d72015-06-24 20:22:23 +00003937FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003938 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3939}
3940
Pete Cooper75403d72015-06-24 20:22:23 +00003941TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003942 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003943}
3944
Pete Cooper75403d72015-06-24 20:22:23 +00003945ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003946 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003947}
3948
Pete Cooper75403d72015-06-24 20:22:23 +00003949SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003950 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003951}
3952
Pete Cooper75403d72015-06-24 20:22:23 +00003953FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003954 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003955}
3956
Pete Cooper75403d72015-06-24 20:22:23 +00003957FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003958 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003959}
3960
Pete Cooper75403d72015-06-24 20:22:23 +00003961UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003962 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003963}
3964
Pete Cooper75403d72015-06-24 20:22:23 +00003965SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003966 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003967}
3968
Pete Cooper75403d72015-06-24 20:22:23 +00003969FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003970 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003971}
3972
Pete Cooper75403d72015-06-24 20:22:23 +00003973FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003974 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003975}
3976
Pete Cooper75403d72015-06-24 20:22:23 +00003977PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003978 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003979}
3980
Pete Cooper75403d72015-06-24 20:22:23 +00003981IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003982 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003983}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003984
Pete Cooper75403d72015-06-24 20:22:23 +00003985BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003986 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003987}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003988
Pete Cooper75403d72015-06-24 20:22:23 +00003989AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003990 return new AddrSpaceCastInst(getOperand(0), getType());
3991}
3992
Pete Cooper75403d72015-06-24 20:22:23 +00003993CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003994 if (hasOperandBundles()) {
3995 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3996 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3997 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003998 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003999}
4000
Pete Cooper75403d72015-06-24 20:22:23 +00004001SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004002 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004003}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004004
Pete Cooper75403d72015-06-24 20:22:23 +00004005VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004006 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004007}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004008
Pete Cooper75403d72015-06-24 20:22:23 +00004009ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004010 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00004011}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004012
Pete Cooper75403d72015-06-24 20:22:23 +00004013InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004014 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004015}
4016
Pete Cooper75403d72015-06-24 20:22:23 +00004017ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00004018 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00004019}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004020
Pete Cooper75403d72015-06-24 20:22:23 +00004021PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00004022
Pete Cooper75403d72015-06-24 20:22:23 +00004023LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00004024 return new LandingPadInst(*this);
4025}
4026
Pete Cooper75403d72015-06-24 20:22:23 +00004027ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00004028 return new(getNumOperands()) ReturnInst(*this);
4029}
4030
Pete Cooper75403d72015-06-24 20:22:23 +00004031BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00004032 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004033}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004034
Pete Cooper75403d72015-06-24 20:22:23 +00004035SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004036
Pete Cooper75403d72015-06-24 20:22:23 +00004037IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004038 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00004039}
4040
Pete Cooper75403d72015-06-24 20:22:23 +00004041InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00004042 if (hasOperandBundles()) {
4043 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
4044 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
4045 }
Devang Patel11cf3f42009-10-27 22:16:29 +00004046 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00004047}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004048
Pete Cooper75403d72015-06-24 20:22:23 +00004049ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00004050
David Majnemer654e1302015-07-31 17:58:14 +00004051CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
4052 return new (getNumOperands()) CleanupReturnInst(*this);
4053}
4054
David Majnemer654e1302015-07-31 17:58:14 +00004055CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00004056 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004057}
4058
David Majnemer8a1c45d2015-12-12 05:38:55 +00004059CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
4060 return new CatchSwitchInst(*this);
4061}
4062
4063FuncletPadInst *FuncletPadInst::cloneImpl() const {
4064 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00004065}
4066
Pete Cooper75403d72015-06-24 20:22:23 +00004067UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00004068 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00004069 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00004070}