blob: 67ce3d26f2b827c696209d5e0373c5f8deb006e2 [file] [log] [blame]
Gabor Greif697e94c2008-05-15 10:04:30 +00001//===-- Use.cpp - Implement the Use class ---------------------------------===//
Gabor Greiff6caff662008-05-10 08:32:32 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Gabor Greiff6caff662008-05-10 08:32:32 +00006//
7//===----------------------------------------------------------------------===//
Gabor Greiff6caff662008-05-10 08:32:32 +00008
Chandler Carruth06d49182014-03-04 08:51:00 +00009#include "llvm/IR/Use.h"
Chandler Carruth387e0592014-03-04 09:19:43 +000010#include "llvm/IR/User.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Value.h"
Douglas Gregorc0f63802012-03-26 14:04:17 +000012#include <new>
Gabor Greiff6caff662008-05-10 08:32:32 +000013
14namespace llvm {
15
Gabor Greif5ef74042008-05-13 22:51:52 +000016void Use::swap(Use &RHS) {
Chandler Carruth4ffd9d22014-03-04 09:00:15 +000017 if (Val == RHS.Val)
18 return;
Gabor Greif5ef74042008-05-13 22:51:52 +000019
Chandler Carruth4ffd9d22014-03-04 09:00:15 +000020 if (Val)
21 removeFromList();
Gabor Greif5ef74042008-05-13 22:51:52 +000022
Chandler Carruth4ffd9d22014-03-04 09:00:15 +000023 Value *OldVal = Val;
24 if (RHS.Val) {
25 RHS.removeFromList();
26 Val = RHS.Val;
27 Val->addUse(*this);
28 } else {
Craig Topperc6207612014-04-09 06:08:46 +000029 Val = nullptr;
Chandler Carruth4ffd9d22014-03-04 09:00:15 +000030 }
31
32 if (OldVal) {
33 RHS.Val = OldVal;
34 RHS.Val->addUse(RHS);
35 } else {
Craig Topperc6207612014-04-09 06:08:46 +000036 RHS.Val = nullptr;
Gabor Greif5ef74042008-05-13 22:51:52 +000037 }
38}
39
Chandler Carruth387e0592014-03-04 09:19:43 +000040unsigned Use::getOperandNo() const {
41 return this - getUser()->op_begin();
42}
43
Gabor Greiff6caff662008-05-10 08:32:32 +000044void Use::zap(Use *Start, const Use *Stop, bool del) {
Jay Foadbbb91f22011-01-16 15:30:52 +000045 while (Start != Stop)
46 (--Stop)->~Use();
47 if (del)
Gabor Greiff6caff662008-05-10 08:32:32 +000048 ::operator delete(Start);
Gabor Greiff6caff662008-05-10 08:32:32 +000049}
50
Alexander Kornienkof00654e2015-06-23 09:49:53 +000051} // End llvm namespace